Java Reference
In-Depth Information
management of session state is particularly difficult. Currently, standard speci-
fications for the various servlet protocols deal with expiration of session state
through timeout. When a session is established, a timeout is specified. When
the timeout expires, the conversation expires as well. Excessive timeout
lengths can cause memory to be exhausted, while too short a timeout can
cause frustrating user experiences.
A similar problem involves the cache. In many cases, a cache can exhaust
memory if the data set is large and older data is not removed periodically. If
the cache is allowed to grow unabated, memory will eventually be exhausted.
With only minor changes, we can easily ensure that cache memory can be
neatly managed.
6.4.2
Solution 1: Search for common warning signs
Collections can serve many different purposes and can be involved in many
different types of leaks, but you can recognize common threads. Table 6.3
describes the common warning signs, along with the appropriate actions.
Table 6.3
Memory leaks are common in applications with certain characteristics.
Warning Sign
Description
Action
Mismatched life cycles
Whenever an object with a long life
cycle references an object with a
short life cycle, there is leak
potential.
Examine objects with long life
cycles. Try to make sure refer-
ences to transient objects are
removed.
Mismatched add/remove
for shared collections
Whenever a shared collection exists,
adds without deletes may provide ref-
erences that prohibit garbage collec-
tions.
Make sure that any add has a
corresponding delete or weak
reference that allows garbage
collection.
Singletons and static
objects
Static objects and singletons have
long life cycles, and have the poten-
tial for memory leaks.
Examine static objects and sin-
gletons to make sure that refer-
ences to transient objects are
removed and commented.
“May” conditions for
registrations
Whenever a registration to a
collection is voluntary (“may” versus
“must”), absence of strict attention
can lead to a memory leak.
Watch voluntary registrations
especially closely, or weaken
the references to allow garbage
collection.
APIs that hide collections
Whenever an API hides an addition to
a collection, it is possible that the
associated remove will be missed.
Comment APIs that hide
collections, and make sure that
responsibilities are clear, or
weaken the references to allow
garbage collection.
Search WWH ::




Custom Search