One of the most important requirements when investigating deadlocks is actually having a deadlock to investigate. In my last blog I wrote some code called
DeadlockDemo that used a bunch of threads to transfer random amounts between a list of bank accounts before grinding to a halt in a deadlock.
This blog runs that code to demonstrates a few ways of obtaining a thread dump. A thread dump is simply a report showing the status of all your application’s threads at a given point in time. The good thing about it is that it contains various bits of information that will allow you to figure out why you have a deadlock and hopefully allowing you to fix it, but more on that later.
If your Java application is running on a UNIX machine the first, and possibly easiest, way to grab hold of a thread dump is to use the UNIX
kill command via a terminal.
To do this, first get hold of your application’s process identifier or PID using the
grep commands. For example if you type:
ps –e | grep java
…then you’ll produce a list that looks something like this:
74941 ttys000 0:00.01 grep java 70201 ttys004 1:00.89 /usr/bin/java threads.deadlock.DeadlockDemo
The PID for
DeadlockDemo is, in this case, 70201 and is taken from the output above. Note that different flavours of UNIX or different
ps command line args can produce slightly different results, so check your
Having got hold of your PID, use it to issue a
kill SIGQUIT command:
kill -3 70201
kill command is the UNIX command that disposes of unwanted processes
Although the -3 above is the SIGQUIT (equivalent to a keyboard ctrl-D) argument, if Java receives this signal it will not quit, it will display a thread dump on its associated terminal. You can then grab hold of this and copy it into a text file for further analysis.
If you’re working in Windows then the UNIX command line isn’t available. To counter this problem Java comes with a utility that performs the equivalent of
kill. This is called
jstack and is available on both UNIX and Windows. It is used in the same way as the
kill command demonstrated above:
Getting hold of a PID in Windows is a matter of opening the Windows Task Manager. Task Manager doesn’t display the PIDs by default and so you need to update its setup by using the
view menu option and checking the PID (Process Identifier) option in the Select Columns dialogue box.
Next, it’s just a matter of examining the process list and finding the appropriate instance of
java.exe’s PID and use it as a
jstack argument as shown below:
Once the command has completed you can grab hold of the output and copy it into a text file for further analysis.
jVisualVM is the ‘Rolls Royce’ way of obtaining a thread dump. It’s provided by Oracle as tool that allows you to get hold of lots of different info about a Java VM. This includes heap dumps, CPU usage, memory profiling and much more.
jVisualVM’s actual program name is
jvisualvm.exe on Windows. Once running you’ll see something like this:
To obtain a thread dump, find your application in the left hand applications panel, then right click and select: “Thread Dump”.
A thread dump is then displayed in jvisualvm’s right-hand pane as shown below:
Note that I have seen jvisualvm hang on several occasions when connecting to a local VM. When this happens ensure that its proxy settings are set to No Proxy
Having obtained a thread dump, my next blog will now use it to investigate the what’s going wrong with the example
For more information see the other blogs in this series.
Reference: Investigating Deadlocks – Part 2: Obtaining the Thread Dump from our JCG partner Roger Hughes at the Captain Debug’s Blog blog.Related Whitepaper:
Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code
Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!
To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.