I've a stand alone application which makes use of VMware's Java Webservice API, this is a wrapper around the webservice client. I'm using a fixed thread pool of size 5 to invoke the APIs in library. After running for a while (a day or two), the memory usage creeps up and the heap dump shows that thread locals of the pooled threads has accumulated lots of memory.
Is there anyway to clear those thread locals? Since the webservice call is actually invoked from a third party library, I'm not able to clear the thread locals directly.
This is a classic problem related to ThreadLocal
. Thread local variables being global thread-local variables (Confined and belongs to the thread), they must not be used with a Thread Pool which retains Threads even after their task is over. This will eventually result in PermGen memory issues.
Since you can not change the ThreadLocal
s in the third party library what you can use is avoid using a Thread Pool and create/start/destroy threads at will for each task. This way you can avoid Threads being alive after its task completion and avoid the unintended memory holding with ThreadLocal
variables.
For more information read this post