Search This Blog

Wednesday, September 15, 2010

Java heap dump

I recently worked on a project where I was faced with "out of memory" problem. Exception appeared usually after the application has worked for several weeks. I solved the problem with VisualVM tool that ships with Java 1.6 JDK.
VisualVM tool provides a direct connection to any Virtual Machine locally or remote, or can load heap dump or thread dump from a file.

To dump heap memory to a file type in console(linux):
  1. Get java process id
    ps -eaf | grep java
  2. Dump heap memory to a file using jmap ("jmap prints shared object memory maps or heap memory details of a given process or core file or a remote debug server")
    jmap -heap:format=b PID (where PID i id of your java process)
  3. Run jvisualvm
    jvisualvm -J-Xmx512M
  4. Load heap.bin file
  5. Find biggest objects by retained size. After file loads you can see every object that was in memory at the time the dump was taken.Click on find biggest objects by retained size and you will get hierarchy of biggest objects in memory.
In this way, I discovered that I have HashMap object in some other singleton object, and the values were added to map, but never removed.


  1. where heap.bin file located in linux operating system.

    1. Option 1. You could find it:
      find / -name "heap.bin" -type f 2>/dev/null

      Option 2.
      It should be located in the working directory of the process, which you can get by:

      1. Get process id:
      ps aux | grep java

      2. Get working directory
      pwdx PID (where PID is what you got from step 1)