Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat complains about a thread not being stopped

I'm getting exceptions about threads being started and not ending or being stopped. The WebappClassLoader complains that these threads will cause a memory leak.

What is happening here?

  1. Is it likely such threads will cause a memory leak? Or just possible?
  2. Is Catalina being overly sensitive?
  3. Is this even a valid programming technique?

(Managing this aspect of a Tomcat application is new to me. But I have to learn it as the webapp is running out of database connections under high load and this needs to be solved. Searching for memory leaks is one of a number of things we're looking into.)

like image 747
staticsan Avatar asked Feb 02 '26 20:02

staticsan


1 Answers

You can read about tricky memory leak issues in Tomcat (and this warning in particular) at http://wiki.apache.org/tomcat/MemoryLeakProtection -- in particular, for the warning you see:

If a webapp creates a thread, by default its context classloader is set to the one of the parent thread (the thread that created the new thread). In a webapp, this parent thread is one of tomcat worker threads, whose context classloader is set to the webapp classloader when it executes webapp code.

Furthermore, the spawned thread may be executing (or blocked in) some code that involves classes loaded by the webapp, thus preventing the webapp classloader from being collected.

So, if the spawned thread is not properly terminated when the application is stopped, the webapp classloader will leak because of the strong reference held by the spawned thread.

The impact of this (in my experience) is limited when you start your web application once, and then simply tear it down. However, in some cases, such as in a continuous integration scenario (where your CI server is repeatedly deploying builds of your application into a Tomcat container that is not restarted), you can quickly run out of memory in the JVM. This will eventually manifest itself as an OutOfMemoryException in your PermGen region (assuming you are using the Sun/Oracle JVM). For more details on PermGen, see http://blogs.oracle.com/fkieviet/entry/classloader_leaks_the_dreaded_java

like image 91
Emil Sit Avatar answered Feb 04 '26 10:02

Emil Sit