Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4455134
Votes 0
Synopsis ThreadLocals and associated values not GC'able until thread GC'able
Category java:classes_lang
Reported Against merlin-beta
Release Fixed
State 11-Closed, duplicate of 4414045, bug
Priority: 4-Low
Related Bugs 4414045
Submit Date 07-MAY-2001
Description
Any java.lang.(Inheritable)ThreadLocals set during the lifetime of a thread,
along with the values associated with those ThreadLocals, cannot be garbage
collected until the thread itself is garbage collected, even if application
code no longer holds references to the ThreadLocals or their values.  This
occurs because java.lang.Thread uses java.util.IdentityHashMap (as opposed to a
"weak" variant of IdentityHashMap) to map ThreadLocals to their associated
values; even if the application no longer references a given ThreadLocal, the
threadLocals/inheritableThreadLocals map of any thread in which the ThreadLocal
was set will retain strong references to both the ThreadLocal and its value,
preventing the ThreadLocal and its value from being GC'ed until the referencing
thread(s) is also GC'ed.  The attached code demonstrates this memory leak.

One would expect, instead, that if application code no longer references a
ThreadLocal, then that ThreadLocal should be eligible for GC independent of the
lifetime of threads in which it was set.  This way, if an application creates
and discards multiple ThreadLocals (or, perhaps more commonly, multiple
objects which each contain ThreadLocals), values associated with the
ThreadLocals will still be reclaimable.
Work Around
N/A
Evaluation
N/A
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang