java - ThreadLocal & Memory Leak -
it mentioned @ multiple posts: improper use of threadlocal
causes memory leak. struggling understand how memory leak happen using threadlocal
.
the scenario have figured out below:
a web-server maintains pool of threads (e.g. servlets). threads can create memory leak if variables in
threadlocal
not removed because threads not die.
this scenario not mention "perm space" memory leak. (major) use case of memory leak?
permgen exhaustions in combination threadlocal
caused classloader leaks.
an example:
imagine application server has pool of worker threads.
kept alive until application server termination.
deployed web application uses static threadlocal
in 1 of classes in order store thread-local data, instance of class (lets call someclass
) of web application. done within worker thread (e.g. action originates http request).
important:
by definition, reference threadlocal
value kept until "owning" thread dies or if threadlocal no longer reachable.
if web application fails clear reference threadlocal
on shutdown, bad things happen:
because worker thread never die , reference threadlocal
static, threadlocal
value still references instance of someclass
, web application's class - even if web application has been stopped!
consequence, web application's classloader cannot garbage collected, means all classes (and static data) of web application remain loaded (this affects permgen memory pool heap).
every redeployment iteration of web application increase permgen (and heap) usage.
=> permgen leak
one popular example of kind of leak this bug in log4j (fixed in meanwhile).
Comments
Post a Comment