How to Obtain Thread and Heap Dump?

Get PID(Process ID) of Java runtime

Get the PID of the running java runtime using the operating system. Use ps command in Linux/Mac/Unix and in windows use Task Manager’s process tab.

Another easy option is to use jps tool provided by JDK. jps lists all java Processes of a user.

Thread Dump

A Java thread dump gives vital information about all the threads in a JVM.  Information includes execution point of each thread and the locks( if any) on which threads are waiting.

Thread dump using JDK jstack

Oracle JDK provides a utility called jstack which can take a thread dump of a running Java application

usage:
jstack -l <PID> > thread.tdump

Eg., jstack -l 413 > thread.tdump  /** 413 is the Java Process ID **/

To take the thread dump in a loop while running load test use the following script:

while true
do
jstack <PID>   >>  stack-`date +%s`.txt
sleep 60
done

Heap Dump

A heap dump is a snapshot of a JVM’s heap space, complete with all object references. This is a (often very large) binary file and needs special tools to be analyzed.

To obtain a heap dump of an existing JVM process on Sun / Oracle JVMs use the jmap tool.
$ jmap -dump:format=b,file=heapdump.jmap <PID>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s