Java Thread Dump - VisualVM, jstack, kill -3, jcmd With Examples

Java Thread dump is list of all the threads active in the JVM.

Java Thread Dump

Java thread dump is very helpful in analyzing bottlenecks in the application and deadlock situations.

Here we will learn multiple ways through which we can generate thread dump for a java program. These instructions are valid for *nix operating systems but in windows the steps might be little different.

  1. VisualVM Profiler: If you are analyzing application for slowness, you must use a profiler. We can generate thread dump for any process using VisualVM profiler very easily. You just need to right click on the running process and click on “Thread Dump” option to generate it.
  2. visualvm-java-thread-dump
  3. jstack: Java comes with jstack tool through which we can generate thread dump for a java process. This is a two step process.
    1. Find out the PID of the java process using ps -eaf | grep java command
    2. Run jstack tool as jstack PID to generate the thread dump output to console, you can append thread dump output to file using command “jstack PID >> mydumps.tdump
  4. We can use kill -3 PID command to generate the thread dump. This is slightly different from other ways to generate thread dump. When kill command is issued, thread dump is generated to the System out of the program. So if it’s a java program with console as system out, the thread dump will get printed on the console. If the java program is a Tomcat server with system out as catalina.out, then thread dump will be generated in the file.
  5. Java 8 has introduced jcmd utility. You should use this instead of jstack if you are on Java 8 or higher. Command to generate thread dump using jcmd is jcmd PID Thread.print.

Above are four different ways to generate thread dump in java. Usually I prefer jstack or jcmd command to generate thread dump and analyze. Note that whatever way you choose, thread dump will always be the same.

Java Thread Dump Example

In my last post, I explained about java Timer, here is a thread dump generated for the same program.

Thread dump is the list of all the threads, every entry shows information about thread which includes following in the order of appearance.

  1. Thread Name: Name of the Thread
  2. Thread Priority: Priority of the thread
  3. Thread ID: Represents the unique ID of the Thread
  4. Thread Status: Provides the current thread state, for example RUNNABLE, WAITING, BLOCKED. While analyzing deadlock look for the blocked threads and resources on which they are trying to acquire lock.
  5. Thread callstack: Provides the vital stack information for the thread. This is the place where we can see the locks obtained by Thread and if it’s waiting for any lock.

That’s all for thread dump in java.

By admin

Leave a Reply

%d bloggers like this: