Search code examples
javalockingprofilingmonitoringdeadlock

Tool to find cause of dead lock in Java application


We can reproduce a situation where part of your system freezes. We suspect that this might be due to a locking problem.

How can we find out where every thread of our application is currently at? Which tool can we use to find the spot where the deadlock occurs?


Solution

  • You need to take a Thread dump of your running process to see stack traces of all the thread running at that time.

    Its a good idea to take multiple Thread dumps in an interval of 5-10 secs so that you can compare the activity of threads in different dumps.

    There are multiple ways to take Thread dump of an application:

    1. If you have jstack on the machine where process is running,

      jstack {pid} > threaddump.log
      
    2. kill -3 {pid}

    3. Using jvisiualvm or jconsole, you can connect to the local/remote process and generate the threaddump