ThreadLocal & Memory Leak

Source: Internet
Author: User

PermGen exhaustionsIn combination withThreadLocalis often caused byClassLoader leaks.

An example:
Imagine an application server which have a pool ofWorker Threads.
They'll be kept alive until application server termination.
A deployed Web application uses aStaticThreadLocalIn one of their classes in order to store some thread-local data, an instance of another class (Lets call itSomeClass) of the Web application. This is do within the worker thread (e.g. this action originates from aHTTP Request).

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

If the Web applicationfails to clear the referenceto theThreadLocalOn shutdown, bad things would happen:
Because the worker thread would usually never die and the reference to theThreadLocalis static, theThreadLocalValuestill referencesThe instance ofSomeClass, a Web application ' s class-even if the Web application has been stopped!

As a consequence, the Web application ' sClassLoader cannot be garbage collected, which means thatAll Classes(and all static data) of the Web applicationremain loaded(This affects the PermGen memory pool as well as the heap).
Every redeployment iteration of the Web application would increase PermGen (and heap) usage.

= = The PermGen leak

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

ThreadLocal & Memory Leak

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.