Quitada ブログ HAX

Hatena Blog でも Quitada ブログ

Tool to find slow memory leak of java applications

What is this tool?
To detect slow memory leak in java heap area, this tool extracts difference of each object numbers between two java heap hisograms before and after leaking memory.

Preparation
Before using this tool, you have to get two java heap histgrams with the following command before and after memory leak.
-----
jmap -histo:live [java process id] > [text filename]
-----

I only test this tool for jmap command bundled in Oracle JVM - especially, Java 6.

How to use this tool
This tool is written in Java. So just execute this as a Java appliction with adding this jar file to CLASSPATH. The usage is similar to UNIX diff command like the following.
-----
java quitada.JavaObjectDiff [java histogram file before memory leak] [java histogram file after memory leak]
-----

You will see the following output showing the difference of the number of each obejcts and sort them in descending order like the following.
-----
    diff    class name
    ----------
    10828 [B
    10244 java.util.concurrent.ConcurrentHashMap$HashEntry
    9872 java.lang.Integer
    5789 java.util.concurrent.locks.ReentrantLock$NonfairSync
    5632 java.util.concurrent.ConcurrentHashMap$Segment
    5632 [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
    2429 <symbolKlass>
       :
    -2 sun.reflect.NativeConstructorAccessorImpl
    -2 java.net.InetAddress$CacheEntry
    -8 java.util.ResourceBundle$CacheKey
    -8 java.util.ResourceBundle$BundleReference
    -8 java.util.ResourceBundle$LoaderReference
-----

The first column indicates the difference of the number of each objects. The second column indicates class name for each objects. Larger the number of differences, the object may cause memory leak. So you can start to detect where causes memory leak based on the class name of the object.

This tool doesn't provide complete solutions to detect memory leak. But this will narrow down the problem domain especially for users who don't have commercial tools to detect memory leak or can't attach those tools in their production system.