Java thread dumps – 7 ways of taking them

A Java thread dump is a snapshot of what all the threads are doing in the JVM at the particular time we send the request of the dump. The Java thread dump results contains a lot of information, that is, each thread in the JVM is listed with its name, id, current state and the Java call stack. There are various ways of getting a thread dump.

1. jstack

jstack tool is shipped with JDK. You can read more about it here : Using jstack to get thread dumps

 

2. JVisualVM

This tool is also shipped with JDK. It is a graphical user interface used to analyze Java applications.You can dig into this tool here : Using jvisualvm to find blocked threads

It has a threads tab where we can trigger the thread dump for the given java process.

 

3. Kill -3

In most of the enterprises, JDK is not installed by default due to security reasons and only JREs are installed in production systems. Above mentioned tools will be missing in such systems. On such systems you can run Kill command:

Kill -3 PID

where PID is the process ID of the java process.

 

4. Windows (Ctrl + Break)

This option will work only in Windows Operating system.

  • Select command line console window in which you have launched application.
  • Now on the console window issue the “Ctrl + Break” command.

This will generate thread dump. A thread dump will be printed on the console window itself.

 

5. ThreadMXBean

We can even write Java code to fetch thread dump

package threads;
 
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
 
public class ThreadBeanThreadDumpExample {
 
	public static void main(String[] args) {
		ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
		for(ThreadInfo tInfo : threadBean.dumpAllThreads(true, true)){
			System.out.println(tInfo);
		}
	}
}

This will print thread dump like this :

“Attach Listener” Id=5 RUNNABLE

“Signal Dispatcher” Id=4 RUNNABLE

“Finalizer” Id=3 WAITING on java.lang.ref.ReferenceQueue$Lock@2a139a55
at java.lang.Object.wait(Native Method)
– waiting on java.lang.ref.ReferenceQueue$Lock@2a139a55
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

“Reference Handler” Id=2 WAITING on java.lang.ref.Reference$Lock@15db9742
at java.lang.Object.wait(Native Method)
– waiting on java.lang.ref.Reference$Lock@15db9742
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)

“main” Id=1 RUNNABLE
at sun.management.ThreadImpl.dumpThreads0(Native Method)
at sun.management.ThreadImpl.dumpAllThreads(ThreadImpl.java:446)
at threads.ThreadBeanThreadDumpExample.main(ThreadBeanThreadDumpExample.java:11)

 

6.  JCMD

 

It is very similar to jstack and can be found in JDK’s bin folder

jcmd PID Thread.print > c:/downloads/dump.txt

 

7. JMC

This is yet another tool which is part of JDK since JDK7U4.  I will be blogging about in details soon……

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *