How to Generate and Analyse Java Heap Dump
Prerequisites
Install and Setup Java - How do I install Java?
Steps
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"); } }
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
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>
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
Analyze the heap dump from the below links:
You can also view the objects and instance counts in the histogram.
Subscribe to my newsletter
Read articles from Code Buzz directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Code Buzz
Code Buzz
Developing apps one step at a time!