Java Reference
In-Depth Information
Collection
C
Collection
creator C
Leaked
object B
Object A
new
collection
new B
init B
register B
dereference
B
life cycle
for B
life cycle
for C
Figure 6.4 In the Leak Collection antipattern, an object with a short life cycle is registered in a
collection with a long life cycle. Here we've shown a UML sequence diagram of such a memory
leak. In this case, object B will be a memory leak, because the reference in collection C won't
allow the garbage collector to free it.
Session state: A well-publicized Tomcat bug turned out not to be a
bug at all, but a session-management problem. By default, session
management was turned on, and many didn't know it. In certain cir-
cumstances, the session dictionary grew until memory was gone. In
general, session state is a good place to find a memory leak.
User interfaces: Most modern user interfaces are collections of windows
and components. Many times, these potentially massive collections have
persistent anchors. Some common places to find singleton anchors
might be print support, font management, and interapplication commu-
nication. Whenever a user interface is attached to an anchor with a long
life cycle, leak conditions are present.
EJB containers: An EJB container is essentially a smart singleton collec-
tion of EJB s.
6.4.1
Causing trouble with caches and session state
Two specialized uses of collection classes are especially troublesome. Session
state captures the conversation context between the client and the server.
Because HTTP is a stateless protocol , the infrastructure or the application must
manage conversation details. The duration of Internet conversations varies
wildly among consecutive communications from a given client, so the
Search WWH ::




Custom Search