How to analyze heap data from .hprof file and use it to reduce memory leaks?

19,260

Solution 1

There are many ways to find the root cause of a memory leak, like using a profiler such as JProfiler and simply applying what is described in this great video. You could also have a look to Eclipse Memory Analyzer also know as MAT that will be able to analyze your heap dump and propose potential causes of your memory leak as you can see in this video (you can find more information about the Suspect Report here). Another way could be to use Java Flight Recorder by applying this approach. Or using JVisualVM using the approach described in this video.

Solution 2

The tool you need for this case is this app:

Memory Analyzer Tool

Just download & start, then load your hprof file. It may take a minute or two depending on the size of your hprof, but then you will be presented with a nice analysis on your memory usage. It is very easy to use, and automatically highlights the potential memory leaks, performs analysis on the data from different angles.

I am using MAT exclusively when I am dealing with non-trivial memory issues, and I solved all of these issues as far as I remember.

Solution 3

Most of the time all you need to know is which are the classes most at fault of chewing up memory. You can use: jmap -histo against a running process, which is handy if it's a large JVM...you don't want to be fiddling around with large heap-dump files. It must be run as the same Linux user that owns the process, so e.g. in Linux you can use:

sudo -u <user> jmap -histo <pid>

Obviously "histo" is short for "histogram". This will dump a histogram to stdout, so you probably want to pipe it to a file for analysis. It'll be sorted by the number of instances * size of instance, so look at the top 10 entries and you might have your answer.

Share:
19,260
Anjan Baradwaj
Author by

Anjan Baradwaj

Passionate programmer with a strong will to learn and help others learn through knowledge dissemination. Problem Solver. Technology Enthusiast. Amateur Blogger.

Updated on June 25, 2022

Comments

  • Anjan Baradwaj
    Anjan Baradwaj almost 2 years

    In recent times, I have encountered java.lang.OutOfMemoryError exception while running an application.

    During one such instance, I was able to get the heap dump using jvisualvm.

    I am able to open the .hprof heap dump file obtained from the heap dump using NetBeans 8.1 IDE but I am not aware of how to analyze the data dump. I'd like to know how to read the dump file and take corrective actions to reduce the out of memory exception from an application perspective.