Friday, July 9, 2010

Detect Deadlock And Dump Threads of a Running Java Process

jstack Utility

This is a utility provided by the JDK to do the following operations
  • Dump the threads of a running java application
  • To find the deadlock in the java application
To use this utility you need to know the process ID of the Java application. To know the process ID of all running Java processes simply use the "jps" command in your command prompt.This shows the PID and the process name.

Choose the appropriate PID for which you want the Thread Dump and run the "jstack" command. like

jstack <PID>

This will output the Thread Dump on console, you can also redirect the output to the file.

To show the Deadlock I run a program which is actually created DeadLock in the application. You can see the application is running in jps command output. Name of the application is "AnotherDeadLock". I took the ThreadDump of this application into a file as specified above and used Samurai tool to analyize the Thread Dump log file. Below is the output.