码迷,mamicode.com
首页 > 其他好文 > 详细

ThreadLocal & Memory Leak

时间:2015-12-19 07:43:19      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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).

ThreadLocal & Memory Leak

标签:

原文地址:http://www.cnblogs.com/Guoyutian/p/5058598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!