Meaningless Drivel is fun!*
The moose likes Tomcat and the fly likes Memory Leak after repeat deploy and undeploy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Memory Leak after repeat deploy and undeploy" Watch "Memory Leak after repeat deploy and undeploy" New topic
Author

Memory Leak after repeat deploy and undeploy

Jianping Wang
Ranch Hand

Joined: May 29, 2010
Posts: 60
I put all jar files in the lib of Tomcat and there are no jars in my web app. After repeated deploy and undeploy with Tomcat Manager, memory leak happened.

How to get rid of this issue?



SCJP 6 with 93%
Oracle Database SQL Expert with 98%
Jianping Wang
Ranch Hand

Joined: May 29, 2010
Posts: 60
Forget to mention: I use Tomcat 6.0.20
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

In short, you can't.

The memory that's 'leaking' is the perm-gen section.
This is where interned strings, constants, and in this case, class definitions are stored.
When you load a Java class the definition is written to a permanent section of the JVM's memory block and is never unloaded.

When you reload an application, Tomcat's custom classloaders re-load every class used by that application; not just the one in jar libs but also the classes that are dynmically generated from your JSPs. Since these are never purged, constant reloading of an application will cause the perm-gen sector to keep growing.

The best answer is to clean restart tomcat from time to time.
You can buy more time by allocating more ram to the perm-gen sector with this switch:
-XX:MaxPermSize=600M

But this only buys you time. If you're reloading apps all the time, you'll need to perform a clean start of Tomcat once in a while.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Kris Schneider
Ranch Hand

Joined: Feb 14, 2001
Posts: 71
Jianping Wang wrote:Forget to mention: I use Tomcat 6.0.20


One thing you should definitely try is upgrading to 6.0.26. There has been a fair amount of recent work targeted at memory leaks...
Kees Jan Koster
JavaMonitor Support
Rancher

Joined: Mar 31, 2009
Posts: 251
    
    5
Dear Jianping,

Upgrade to the latest Tomcat and enable its permgen leak detector: http://java-monitor.com/forum/showthread.php?t=818

Also, put the JAR files back into the WAR. Giving them to Tomcat makes it harder to manage their versions and impossible for Tomcat to unload them.

Kees Jan


Java-monitor, JVM monitoring made easy <- right here on Java Ranch
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16158
    
  21

Kees Jan Koster wrote:
Also, put the JAR files back into the WAR. Giving them to Tomcat makes it harder to manage their versions and impossible for Tomcat to unload them.


You also risk massive data corruption and system failure. A Jar in the TOMCAT lib directory is on the classpath of every deployed application and therefore its resources are shared with each and every thread in each and every deployed webapp. If the jar wasn't designed for this kind of multi-threaded use, there's a possibility that the apps will corrupt the jar class resources.

Certain types of jars should be stored in TOMCAT/lib, such as most database driver jars. They're designed to be shared, and in fact, will better co-ordinate resources when placed in the common library directory. But a lot of jars aren't. When in doubt, don't.


Customer surveys are for companies who didn't pay proper attention to begin with.
Jianping Wang
Ranch Hand

Joined: May 29, 2010
Posts: 60
You are absolutely right!!!
I put jar in web app and the leak issue disappeared after change to Tomcat 6.0.26
Tudor Raneti
Ranch Hand

Joined: Nov 29, 2009
Posts: 145
I upgraded to 6.0.29 and my Tomcat still gets the PermGen when it reaches ~111MB
I've set this w/o any effect in system environment variables and eclipse launch configuration environment:
CATALINA_OPTS -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -XX:MaxPermSize=512m -Xms512m -Xmx512m

Seems that every time i clean-build the project, triggering an autopublish and context reload, and after i 'refresh' the app in the browser with a button calling close() on application, Tomcat loads the app in memory, but it doesn't clean the old, so each time i do this it goes up another 15MB. At about 112MB i get a PermGen error. How to fix this please?

I also had this (is that so dire that it leaves behind 15MB every time, as much as the whole app?):
Jul 27, 2010 6:42:57 AM org.apache.catalina.core.StandardContext reload
INFO: Reloading this Context has started
Jul 27, 2010 6:42:57 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/realloto] registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Jul 27, 2010 6:42:57 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/realloto] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak.
Jul 27, 2010 6:42:57 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/realloto] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] but has failed to stop it. This is very likely to create a memory leak.
Jul 27, 2010 6:42:57 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/realloto] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1] but has failed to stop it. This is very likely to create a memory leak.
Jul 27, 2010 6:42:57 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/realloto] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] but has failed to stop it. This is very likely to create a memory leak.
Jul 27, 2010 6:42:57 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/realloto] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.
Ivan Sabolic
Greenhorn

Joined: Jul 18, 2013
Posts: 1
This is very common in Windows environment but there is a solution :

Open context.xml from /tomcat/conf folder and modify the context to match this :

<Context antiJarLocking="true" antiResourceLocking="true">


http://stackoverflow.com/questions/14873219/cannot-undeploy-a-web-app-completely-in-tomcat-7
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Memory Leak after repeat deploy and undeploy