JVM Thread/Heap Dump and Analysis - Getting Started

Heap dump analysis are the essential part of troubleshooting JVM based application which includes Tomcat Apps, JBoss apps etc. Since an application runs inside a JVM container, heap dump provides a memory snapshot of the application at a particular period of a time. Heap dump also consists of thread dump of all the threads running inside the JVM.

JVM Thread dump provides

  • Thread Name
  • Thread Type and Priority
  • Java Thread ID
  • Native Thread ID
  • Java Thread Stack Trace
  • Java Heap breakdown

There are various tools used for Java Heap Analysis. Amongst them Jmap and JHat are few of tools useful for analyzing memory consumption of java program. Both are included in the JVM 1.6 and higher.

Memory Dumps (Heap Dump)

1. Using JVM Argument

By Adding

-XX:+HeapDumpOnOutOfMemoryError, JVM can trigger an automatic heap dump on an event of Out of Memory Error. You can also trigger a dump of the Java heap specifying Heap Dump Path as well using -XX:HeapDumpPath=

2. Using jMap

Using jmap (Memory Map), you can create a dump of the Java memory heap at any moment inf the life of your running application. It will contain all the live objects and classes at that moment.

http://docs.oracle.com/javase/6/docs/technotes/tools/share/jmap.html

Usage: jmap -heap:format=b -dump:file=<file path> <pid>

Example: jmap -heap:format=b -dump:file/home/hdump/test.bin 1234

jmap -heap <pid>

3. jConsole / visuamVM

Using visuamVM or jConsole to attach JVM processes we can take a Heap Dump of that particular process at that point of time.

To activate JMX remote monitoring you must set the following system properties when you start the Java VM.

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

After you have enabled the JMX agent for remote use, you can monitor your application using JConsole or visualVM


Thread Dumps

1. jstack

Once you have pid of the process. We can use jstack to get Thread dump of process. jStack prints Java stack traces of Java threads for a given Java process.

jstack -l <pid>

http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jstack.html

2. kill command

Using kill -3 We can get a thread dump of a process to it's log file.


Heap Analysis

Once you have heap dump, you can use jhat (Java Heap Analysis Tool) to analyze heap dump file. jhat creates an HTTP/HTML server that can be viewed in a web browser. OQL (Object Query Language) is supported by jhat. Sometimes it is quite often to get an OutOfMemory exception when opening java heap, because the dump file can be very memory consuming. If you experience this problem, you can specify heap usage for jhat by passing memory parameter

http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html

Usage

jhat -J mx2000m stack.bin

[Default port for jhat is 7000]

Thread Analysis

TDA (Thread Dump Analyzer) is a very useful tool for thread dump.

https://java.net/projects/tda/

Beside that you can also use Linux top command and switch to Thread Mode by pressing “CTRL + H”. And get list of high CPU consuming thread, do a backtrack of that particular tread from the Thread Dump.