Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThreadLocal & Memory Leak

It is mentioned at multiple posts: improper use of ThreadLocal causes Memory Leak. I am struggling to understand how Memory Leak would happen using ThreadLocal.

The only scenario I have figured out it as below:

A web-server maintains a pool of Threads (e.g. for servlets). Those threads can create memory leak if the variables in ThreadLocal are not removed because Threads do not die.

This scenario does not mention "Perm Space" memory leak. Is that the only (major) use case of memory leak?

like image 691
Sandeep Jindal Avatar asked Jul 31 '13 11:07

Sandeep Jindal


People also ask

What is ThreadLocal used for?

The ThreadLocal class is used to create thread local variables which can only be read and written by the same thread. For example, if two threads are accessing code having reference to same threadLocal variable then each thread will not see any modification to threadLocal variable done by other thread.

Is ThreadLocal safe?

It's safe because getMap returns the map for the given (i.e. current) thread. No other thread is going to be messing with that.

What is ThreadLocal variable in Java?

The Java ThreadLocal class enables you to create variables that can only be read and written by the same thread. Thus, even if two threads are executing the same code, and the code has a reference to the same ThreadLocal variable, the two threads cannot see each other's ThreadLocal variables.

What are ThreadLocal and Threadpool?

ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or transaction ID). The use of ThreadLocal objects requires care in classes whose objects are required to be executed by multiple threads in a thread pool.


1 Answers

PermGen exhaustions in combination with ThreadLocal are often caused by classloader leaks.

An example:
Imagine an application server which has a pool of worker threads.
They will be kept alive until application server termination.
A deployed web application uses a static ThreadLocal in one of its classes in order to store some thread-local data, an instance of another class (lets call it SomeClass) of the web application. This is done within the worker thread (e.g. this action originates from a HTTP request).

Important:
By definition, a reference to a ThreadLocal value is kept until the "owning" thread dies or if the ThreadLocal itself is no longer reachable.

If the web application fails to clear the reference to the ThreadLocal on shutdown, bad things will happen:
Because the worker thread will usually never die and the reference to the ThreadLocal is static, the ThreadLocal value still references the instance of SomeClass, a web application's class - even if the web application has been stopped!

As a consequence, the web application's classloader cannot be garbage collected, which means that all classes (and all static data) of the web application remain loaded (this affects the PermGen memory pool as well as the heap).
Every redeployment iteration of the web application will increase permgen (and heap) usage.

=> This is the permgen leak

One popular example of this kind of leak is this bug in log4j (fixed in the meanwhile).

like image 125
MRalwasser Avatar answered Sep 22 '22 18:09

MRalwasser