JAVA - Generating JVM Heap Dump and Viewing them

To create a JVM heap dump of a running Java process, it is rather easy. But first, you will need to find the JVM process id. To do this, you should use jps

Below is an example of the jps command

$ jps
26722 web.jar
2696 my_java.jar
28931 Jps

The number is pid. You can then use it to create the JVM Heap Dump with jmap

The simple jmap command is

jmap -dump:live,file=/path/to/dump/file XXX

where
XXX: is the Java Process Id, whose heap dump should be captured
/path/to/dump/file: is the file path where heap dump will be written in to.

live option in the -dump switch is important as it will tell the jmap to only dump objects that currently have memory reference. If you omit the live option, jmap will dump everything, including the objects that had already been garbage collected.

An example jmap command 

$ cd /tmp
$ jmap -dump:live,file=my_java.bin 2696
Dumping heap to /tmp/my_java.bin ...
Heap dump file created


Next, you could use jhat to analyze the heap dump. It will read the dump file and launch a HTTP server so that you can analyze the heap dump via the web page

The simple jhat command is

jhat /path/to/dump/file

An example jhat command is

$ jhat my_java.bin
Reading from my_java.bin...
Dump file created Tue Jan 29 21:38:09 EST 2019
Snapshot read, resolving...
Resolving 121841 objects...
Chasing references, expect 24 dots........................
Eliminating duplicate references........................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

You could now access the heap dump via http://host:7000

Honorable mention, you could also use jstack and jconsole to capture the current process thread stack and monitor the live Java process respectively.

Comments

Popular Posts