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>