0

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.

hanish.kh
  • 517
  • 3
  • 15

1 Answers1

2

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 ThreadLocals 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

shazin
  • 21,379
  • 3
  • 54
  • 71
  • Thanks Shazin for the answer. Just one more question, instead of getting the expected PermGen error, I'm getting heap related error. – hanish.kh Dec 18 '14 at 08:45
  • Yes when the heap becomes full enough you will get PermGen Memory Error eventually. – shazin Dec 18 '14 at 10:46
  • How heap getting filled can cause PermGen error? PermGen is not part of heap – hanish.kh Dec 18 '14 at 11:46
  • That is because If an object is supposed to live over process exist then object is moved to PermGen. As in this case The heap space acquired by ThreadLocal variables are long lived over their process. – shazin Dec 18 '14 at 12:05
  • Sorry, but that case applies for container where different class loaders are used for each context/deployed application. But this is a standalone application. – hanish.kh Dec 18 '14 at 12:23