ThreadLocal Memory Leak

本文探讨了 PermGen 泄露问题及其与 ThreadLocal 的关联性,特别是在应用服务器中部署 Web 应用时可能出现的 classloader 泄露问题。当 Web 应用未能正确清理 ThreadLocal 中的数据,可能导致永久代内存泄露,进而引发一系列问题。
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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值