How to find a Java Memory Leak

后端 未结 11 853
遇见更好的自我
遇见更好的自我 2020-11-22 07:48

How do you find a memory leak in Java (using, for example, JHat)? I have tried to load the heap dump up in JHat to take a basic look. However, I do not understand how I am s

相关标签:
11条回答
  • 2020-11-22 07:53

    You really need to use a memory profiler that tracks allocations. Take a look at JProfiler - their "heap walker" feature is great, and they have integration with all of the major Java IDEs. It's not free, but it isn't that expensive either ($499 for a single license) - you will burn $500 worth of time pretty quickly struggling to find a leak with less sophisticated tools.

    0 讨论(0)
  • 2020-11-22 07:53

    You can find out by measuring memory usage size after calling garbage collector multiple times:

    Runtime runtime = Runtime.getRuntime();
    
    while(true) {
        ...
        if(System.currentTimeMillis() % 4000 == 0){
            System.gc();
            float usage = (float) (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024;
            System.out.println("Used memory: " + usage + "Mb");
        }
    
    }
    

    If the output numbers were equal, there is no memory leak in your application, but if you saw difference between the numbers of memory usage (increasing numbers), there is memory leak in your project. For example:

    Used memory: 14.603279Mb
    Used memory: 14.737213Mb
    Used memory: 14.772224Mb
    Used memory: 14.802681Mb
    Used memory: 14.840599Mb
    Used memory: 14.900841Mb
    Used memory: 14.942261Mb
    Used memory: 14.976143Mb
    

    Note that sometimes it takes some time to release memory by some actions like streams and sockets. You should not judge by first outputs, You should test it in a specific amount of time.

    0 讨论(0)
  • 2020-11-22 07:56

    Most of the time, in enterprise applications the Java heap given is larger than the ideal size of max 12 to 16 GB. I have found it hard to make the NetBeans profiler work directly on these big java apps.

    But usually this is not needed. You can use the jmap utility that comes with the jdk to take a "live" heap dump , that is jmap will dump the heap after running GC. Do some operation on the application, wait till the operation is completed, then take another "live" heap dump. Use tools like Eclipse MAT to load the heapdumps, sort on the histogram, see which objects have increased, or which are the highest, This would give a clue.

    su  proceeuser
    /bin/jmap -dump:live,format=b,file=/tmp/2930javaheap.hrpof 2930(pid of process)
    

    There is only one problem with this approach; Huge heap dumps, even with the live option, may be too big to transfer out to development lap, and may need a machine with enough memory/RAM to open.

    That is where the class histogram comes into picture. You can dump a live class histogram with the jmap tool. This will give only the class histogram of memory usage.Basically it won't have the information to chain the reference. For example it may put char array at the top. And String class somewhere below. You have to draw the connection yourself.

    jdk/jdk1.6.0_38/bin/jmap -histo:live 60030 > /tmp/60030istolive1330.txt
    

    Instead of taking two heap dumps, take two class histograms, like as described above; Then compare the class histograms and see the classes that are increasing. See if you can relate the Java classes to your application classes. This will give a pretty good hint. Here is a pythons script that can help you compare two jmap histogram dumps. histogramparser.py

    Finally tools like JConolse and VisualVm are essential to see the memory growth over time, and see if there is a memory leak. Finally sometimes your problem may not be a memory leak , but high memory usage.For this enable GC logging;use a more advanced and new compacting GC like G1GC; and you can use jdk tools like jstat to see the GC behaviour live

    jstat -gccause pid <optional time interval>
    

    Other referecences to google for -jhat, jmap, Full GC, Humongous allocation, G1GC

    0 讨论(0)
  • 2020-11-22 07:56

    There are tools that should help you find your leak, like JProbe, YourKit, AD4J or JRockit Mission Control. The last is the one that I personally know best. Any good tool should let you drill down to a level where you can easily identify what leaks, and where the leaking objects are allocated.

    Using HashTables, Hashmaps or similar is one of the few ways that you can acually leak memory in Java at all. If I had to find the leak by hand I would peridically print the size of my HashMaps, and from there find the one where I add items and forget to delete them.

    0 讨论(0)
  • 2020-11-22 07:57

    Well, there's always the low tech solution of adding logging of the size of your maps when you modify them, then search the logs for which maps are growing beyond a reasonable size.

    0 讨论(0)
  • 2020-11-22 07:59

    Checkout this screen cast about finding memory leaks with JProfiler. It's visual explanation of @Dima Malenko Answer.

    Note: Though JProfiler is not freeware, But Trial version can deal with current situation.

    0 讨论(0)
提交回复
热议问题