Java Reference
In-Depth Information
Leak Collections
D ESCRIPTION : If a collection has a long life cycle, it can have long-lived
references that are never removed. These will prevent large blocks of
memory from being freed.
R EFACTORED SOLUTION NAME : Weak References, or Pairing Add with
R EFACTORED SOLUTION DESCRIPTION : One solution to this problem is
to explicitly remove the reference from the collection. For clarity, add and
remove objects in pairs. If this cannot be done in the same method, the
two methods should be in close proximity. Another solution is to weaken
the reference with Java weak reference objects.
T YPICAL CAUSES : Programming hygiene is a common cause. When adds
are not placed in close proximity to removes, it is easy to neglect to
remove, because the symptoms are delayed.
A NECDOTAL EVIDENCE : “I didn't know you could have a memory leak in
Java.” “The system gets slower and slower, and then it hangs or traps.”
S YMPTOMS , CONSEQUENCES : Some objects are not garbage collected,
even though their primary user is. This leak will cause the system to slow
over its life cycle, until it's terminated or eventually dies.
S OLUTION ALTERNATIVES : Remove the reference appropriately or
shorten the life cycle of the registry. Adding a listener without a weak ref-
erence or a corresponding remove is possible if the life cycle of the registry
is short, but this is vulnerable to changes in life cycle and cut and paste.
Search WWH ::

Custom Search