Catalina.out Memory leak error

Rookie picture Rookie · Dec 30, 2011 · Viewed 11.8k times · Source

I still see this error in tomcat/logs/catalina.out.

Dec 29, 2011 4:04:36 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/LoggingMonitor] appears to have started a thread named [Timer-1] but has failed to stop it. This is very likely to create a memory leak.
Dec 29, 2011 4:04:36 PM org.apache.coyote.http11.Http11Protocol destroy
INFO: Stopping Coyote HTTP/1.1 on http-8180

Is it worth considering and if it is, how can I correct this?

Answer

Bob Kuhar picture Bob Kuhar · Dec 30, 2011

This is probably no big deal (just kill -9 or something) and easy enough to fix. Just figure out which webapp in running on a context of /LoggingMonitor then grep its codebase for...

new Timer();

...and replace them all with...

new Timer( true );

java.util.Timer by default does not run in daemon threads. You need any Timers in your webapps to run on daemon threads (otherwise the container is unable to shutdown properly as it is waiting for the Timer thread to end, which it never does). Find all the "new Timer()" calls and replace them with "new Timer( true )" and the logging complaint should stop.

Spend some time in the JavaDocs learn something about daemon vs non daemon Threads: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html

When I'm working in webapps, I always use daemon threads if I end up doing any of my own multithreading. With the facilities in java.util.concurrent, this is becoming very rare (having to do my own Threading stuff).

Finally, and for the record, I hate java.util.Timer and always recommend using something like ScheduledExecutor to do periodic, repetitive tasks. Its too easy to screw up in Timer and take out the Tread it executes on, daemon or otherwise.