PermGen exhaustionsIn combination withThreadLocal
is often caused byClassLoader leaks.
An example:
Imagine an application server which have a pool ofWorker Threads.
They'll be kept alive until application server termination.
A deployed Web application uses aStaticThreadLocal
In one of their classes in order to store some thread-local data, an instance of another class (Lets call itSomeClass
) of the Web application. This is do within the worker thread (e.g. this action originates from aHTTP Request).
Important:
By definition, a reference to aThreadLocal
valueis kept until the ' owning ' thread dies or if the ThreadLocal itself is no longer reachable.
If the Web applicationfails to clear the referenceto theThreadLocal
On shutdown, bad things would happen:
Because the worker thread would usually never die and the reference to theThreadLocal
is static, theThreadLocal
Valuestill referencesThe instance ofSomeClass
, a Web application ' s class-even if the Web application has been stopped!
As a consequence, the Web application ' sClassLoader cannot be garbage collected, which means thatAll Classes(and all static data) of the Web applicationremain loaded(This affects the PermGen memory pool as well as the heap).
Every redeployment iteration of the Web application would increase PermGen (and heap) usage.
= = The PermGen leak
One popular example of this kind of leak are this bug in log4j (fixed in the meanwhile).
ThreadLocal & Memory Leak