Tomcat Shutdown Memory Leak

Environement

JDK 1.6.0.45

Tomcat 7.0.70

JDBC ojdbc6 (11.2.0.1)

Symptom

When shutdown Tomcat, memory leak messages occur.

The web application [/MyWeb] registered the JDBC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
The web application [/MyWeb] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@1a3da69]) and a value of type [java.lang.Class] (value [class oracle.sql.AnyDataFactory]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
The web application [/MyWeb] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@58431c]) and a value of type [java.lang.Class] (value [class oracle.sql.TypeDescriptorFactory]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

These three objects cannot be garbage collected, so the memory cannot be freed up, and it may finally cause out of memory exception.

Solution 1

Move ojdbc6.jar to Tomcat/lib folder (remove from WEB-INF/lib)

Solution 2

Using ojdbc14.jar (10.2.0.4) and keep it in WEB-INF/lib. No TheadLocal problem. Although JDBC memory leak message still occurs, it will not cause out of memory exception even if we reload the app many times.

Write a ContextListener to deregister JDBC driver only solve JDBC memory leak, but useless for ThreadLocal problem. ThreadLocal memory leak will cause out of memory problem.

Reference

http://javarevisited.blogspot.tw/2013/01/threadlocal-memory-leak-in-java-web.html

http://java.jiderhamn.se/2012/01/29/classloader-leaks-iv-threadlocal-dangers-and-why-threadglobal-may-have-been-a-more-appropriate-name/

https://blog.edgewater.com/2013/01/03/hunting-down-an-apache-permgen-issue/

http://stackoverflow.com/questions/3320400/to-prevent-a-memory-leak-the-jdbc-driver-has-been-forcibly-unregistered

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s