Memory leak in j.l.ThreadLocal
Doug Lea
dl at cs.oswego.edu
Sat Oct 26 18:14:17 UTC 2013
On 10/26/2013 10:54 AM, Victor Polischuk wrote:
> There is a quite old problem within ThreadLocal semantic which causes memory
> leak in case an object put in ThreadLocal has hard reference (direct or
> indirect) to ThreadLocal itself.
This could be considered a leak in that the spec/javadoc doesn't
sufficiently emphasize that if a ThreadLocal includes a cyclic
reference chain, then, in general, for a long-lived thread,
you must call ThreadLocal.remove to clear it. (That is why
method ThreadLocal.remove was introduced.)
As you (and Andrew) point out, this is a long-standing
known issue. There is also a known solution, in fact only
one form of solution known to work in general: Ephemerons,
basically a new, special kind of WeakReference. (Try
searching "java ephemeron" for discussion on various lists and forums).
It would be swell if we had them. Maybe someone could open up
a JDK9 JEP for them. But short of that, as Andrew implies,
nothing is known to be better than what's there.
-Doug
More information about the core-libs-dev
mailing list