How to Generate and Analyse Java Heap Dump

How to Generate and Analyse Java Heap Dump

Java

·

1 min read

Prerequisites

Install and Setup Java - How do I install Java?

Steps

  1. Write and execute a simple Java program.

    
      public class SimpleJavaProgram {
         public static void main(String[] args) {
                List<String> list = null;
                while(true) {
                       list = new ArrayList(); //Memory leak
                       System.out.println(list);
                }
                System.out.println("Done");
         }
     }
    
  2. Go to cmd and execute "jps" to find the JVM ID(vmid) of the running application. JPS stands for Java Virtual Machine Process Status Tool and this tool is used to list the JVMs runningn on the system.

     jps
    
  3. Now run the "jmap" command to generate the heap dump using the above vmid.JMap stands for Java Memory Map and returns the heap memory details.

     jmap -dump:file=heapDump.jmap <vmid>
    
  4. Run "jhat" on the heap dump generated to analyze the heap dump. Jhat stands for Java Heap Analysis Tool. It is used to analyze and monitor the heap dump.

     jhat heapDump.jmap
    
  5. Analyze the heap dump from the below links:

    http://localhost:7000/

    You can also view the objects and instance counts in the histogram.

    http://localhost:7000/histo/