4

I have a web service developped by spring/hibernate, and when I do deployment and undeployment on Tomcat 7 many times, I get an outOfMemoryError PermGen Memory leak.

By using java VisualVM, I noticed that the previous webappClassLoader are still kept in JVM, so they are not garbage collected.

What can be the reasons of this problem ?

How can I detect which objects still have reference to the application class loader or any object loaded by it ?

Thanks,

nouro
  • 228
  • 2
  • 7

1 Answers1

4

The short answer is that you have a memory leak. From experience, this is most likely to be in your web application or a library it is using. It is also possible, but unlikely, that you have found a memory leak in Tomcat.

The short version for tracking down the memory leak is:

  • Start Tomcat
  • Undeploy and redeploy the problematic application once
  • Use a profiler to examine the heap
  • Look for an instances of the WebappClassLoader
  • Find the one that is strongly held but has an attribute of started = false
  • Trace the GC roots for that instance of the WebappClassLoader
  • That will point to your memory leak

Finding the root cause of the memory leak might be a little harder.

For a fuller explanation of how to track down this sort of memory leak and what might cause them see this presentation: http://people.apache.org/~markt/presentations/2010-08-05-Memory-Leaks-JavaOne-60mins.pdf

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60
  • this would get +1 just because of the presentation link. Then I noticed you are actually its author. Best material I've read on the subject and I now wonder how come it's the first time I read it. Thank you :) – dkateros Jun 19 '14 at 11:09
  • thanks ! as you said I got instances of WebappClassLoader which have started = false. the problem is to trace the GC root ! I suspect a TimerThread which isn't stopped ! so how can I do for stop this thread after web app undeployment ? – nouro Jun 19 '14 at 13:35
  • Use a ServletContextListener and stop the thread in the contextDestroyed() event. – Mark Thomas Jun 24 '14 at 12:53