how to configure tomcat so that classes from previous runs don't remain in memory

Viewed 887

On tomcat/manager when I click on find leaks button I get the following message:

The following web applications were stopped (reloaded, undeployed), but their classes from previous runs are still loaded in memory, thus causing a memory leak (use a profiler to confirm):

This is actually causing big problems for me because when I need to re-deploy an app with some changes, the old version remains in the server and when I try to use it I get:

IllegalStateException: Illegal access: this web application instance has been stopped already

Even though in tomcat/manager the application shows as started.

How do I resolve this? I looked at here but did not solve the problem. I tried running jps command to get pid of JVM but it doesn't return the JVMs, I guess due to permissoin issue.

Is it possible to configure tomcat somehow so that when an application is undeployed, it shouldn't keep any classes of that application in the memory?

The question How to purge tomcat's cache when deploying a new .war file? Is there a config setting? is not solving my problem as I followed the steps given there:

  • undeploy the app
  • stop tomcat
  • delete the app from `work` directory of tomcat
  • clear browser cache
  • start tomcat
  • put the war file in `webapps`
  • wait a few moments
  • start the app

those steps didn't solve the problem

1 Answers

There is no way to configure tomcat to drop classes that are still referenced. An easy way (probably not the only one) to cause this is to start a thread from your webapplication and not terminate it when the app is undeployed: To tomcat, this thread is then unavailable, but for the JVM, the running thread is a valid root for classes that can't be garbage collected.

Tomcat does its best to detect and indicate such a condition, but really can't go against the JVM and garbage collector and discard perfectly valid objects that are still referenced.

The steps you give, including stopping and starting tomcat, seem impossible: Once you terminate the JVM, the only chance for a newly started JVM to reference the old classes is if it gets hold of them. But you also state that you're deleting the work directory (what about the temp directory?). You should also make sure that you're actually deleting the right content, not some other server's content in a similar directory. Other than that: It seems you're just missing an almost obvious place where the old classes are located. Once they're picked up on restart, the condition that I'm describing above might kick in. There's no way around implementing the webapp to behave nicely and not start random background threads without terminating them.

You might want to check if CATALINA_HOME and CATALINA_BASE are different and might give a hint about other applications deployed under the same name, and if your application gets started when you just delete the files you describe, then start (without new deployment)

Related