7

when stop my project , tomcat say :

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) .

Where we find that Which classes are left in memory ?

please help me .

Ali Farozi
  • 193
  • 2
  • 2
  • 6

2 Answers2

12

You can run jmap -histo which will show you loaded classes.

For example:

jmap -histo[:live] <pid>
    to connect to running process and print histogram of java object heap
    if the "live" suboption is specified, only count live objects

Example: jmap -dump:live,format=b,file=heap.bin <pid>

Another way is to enable classloading debug information and do some scripting to detect what is left loaded.

Taras Melnyk
  • 3,057
  • 3
  • 38
  • 34
mindas
  • 26,463
  • 15
  • 97
  • 154
  • @ mindas,What is the book jmap -histo ? how download it ? and how enable classloading debug information ? – Ali Farozi May 26 '12 at 03:07
  • jmap comes with your JDK, there's no need to download anything. As for verbose loading, add `-verbose:class` option to your java command. – mindas May 26 '12 at 08:50
  • jmap is a command line tool, you can not access it through webapp. pid is your Java process id. – mindas May 26 '12 at 15:46
  • thanks ,I ran jmap -histo . How to understand ,Why my class remain in memory ? – Ali Farozi May 26 '12 at 17:42
  • All of the listed ones are in memory. – mindas May 26 '12 at 20:29
  • I mean, that remains why the memory ?What is the problem of class ? – Ali Farozi May 27 '12 at 04:27
  • Well, there is no single/simple answer to this. You need to study the documentation of Tomcat or your classloader to explain this. – mindas May 27 '12 at 13:30
  • Which part in documentation of Tomcat ? Where to find documentation classloader ? – Ali Farozi May 27 '12 at 13:34
  • You can have a look at this http://zeroturnaround.com/blog/rjc201/ which has more detailed explanation why classloader leaks happen. For Tomcat classloader, simply google and you will find the relevant article. – mindas May 27 '12 at 14:33
8

When this happens, generally there is something in memory which is "pinning" the WebappClassLoader (the class loader that is responsible for loading classes for an instance of your webapp) into memory. If you re-load your webapp several times, you will be able to see the number of WebappClassLoader instances increasing by 1 for every reload that you perform. This is usually not a leak in Tomcat but instead a leak either directly in your webapp's code, a leak in a library that you use, or a leak triggered by a few specific Java API calls that do stupid things.

First, please read this: http://people.apache.org/~markt/presentations/2010-11-04-Memory-Leaks-60mins.pdf. It's an excellent description of what exactly is happening.

Second, use a profiler to determine what is holding references to objects loaded by your WebappClassLoader. Lots of times, simply using a ServletContextListener can clean-up those references as the webapp is being stopped.

Third, if you find that there is a leak coming from a library you use, please let them know about it. If you find a leak coming from a class in the JRE, then look at the options for using the JreMemoryLeakPreventionListener: http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html#JRE_Memory_Leak_Prevention_Listener_-_org.apache.catalina.core.JreMemoryLeakPreventionListener. If there is no option for whatever you have found, please drop-by the Tomcat users' and let us know what's missing: we'll add it.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • thanks,how setting a profiler to determine what is holding references to objects loaded by your WebappClassLoader ? – Ali Farozi May 26 '12 at 03:09
  • If you're asking how to use a profiler to investigate these problems, read page 11 from the link above. – Christopher Schultz May 26 '12 at 05:01
  • Do you mean 'Profiler'? A Profiler is a tool that allows you to inspect a running application -- usually either the CPU or memory usage (or both). There are many profilers available for Java. Just Google for "Java Profiler". I happen to use YourKit Java Profiler because they give me a free license due to my association with the ASF. – Christopher Schultz May 29 '12 at 14:36