Clearly, you can use sophisticated Java analysis tools (such as JProfiler) for such a job. However, there are instances where you simply don't have access to such tools. For example, if you are supporting a remote customer and all you have is the ability to ask the customer to use "jstack" or to send a SIGQUIT signal to the Java app process. You usually cannot ask a customer to stop an app and restart it with the appropriate debugging system properties and options and then connect to it with a tool like JProfiler (which also assumes they even have such a tool installed and available).
So, what you normally have to fall back on is obtaining a thread dump (which is nothing more than normal text). Once that thread dump is captured, you can save it as a .txt file, send it around via email, post it via pastebin or even send it over IM (if its small enough).
A simple way you can get a thread dump is via the "jstack" utility that ships with the JDK. Once you know the pid of your Java JVM process (to get this information, you can use the standard "ps" utility or you can use "jps", which is another JDK utility), you simply tell "jstack" to output a thread dump associated with that process which you redirect to a file:
# find the process IDs of all of my running Java applications $ jps 21147 Jps 16640 Main # take an initial thread dump snapshot of my "Main" Java application $ jstack 16640 > /tmp/my-thread-dump.txt # take a second thread dump snapshot and append it to the original $ jstack 16640 >> /tmp/my-thread-dump.txt
The question then is - how do you analyze it? I recently came across a couple of small, free, easy-to-install, easy-to-run GUI tools that help assist in analyzing thread dumps. The first is Samurai and the second is TDA (Thread Dump Analyzer). They are easy to download and install:
- Samurai is downloaded as a simple samurai.jar file that you start via "java -jar samurai.jar"
- TDA is downloaded as a zip file that you unzip which again gives you a simple tda.jar that you start via "java -jar tda.jar"
Each tool has basically the same way to start - you just tell it to open up a text file that contains one or more thread dumps in them (such as the "my-thread-dump.txt" file that my example above captured).
Each tool has its own way of displaying the threads:
Samurai Main Screen That Shows Threads and Their States |
TDA Main Screen That Shows Threads and Their States |
Its fairly obvious how to use these tools, so I won't bore you with details. Just click around, and you'll get it fairly quickly. They both have relatively intuitive user interfaces. There isn't much too them - don't expect any artificial intelligence to analyze your threads - but you can use them to navigate through the stack traces of all the threads easier than scrolling up and down the base text file.
Notice also that both tools can analyze multiple thread dumps if it detects more than one in your thread dump text file (in my snapshots above, I had three thread dumps). It is excruciating trying to do this by hand (scrolling through multiple thread dumps in a single file) so this is when these tools are really of great help. I also like how Samurai, in one view, shows the states of the threads that are common in the multiple thread dumps. So you can see which threads were running, blocked, etc and which ones changed states between the different thread dumps. However, Samurai loses points because, if thread names are really long, you are forced to scroll horizontally to see the names of the shorter-named threads and to see the states.
One final thing I wanted to show is how they can quickly point out the threads that are deadlocked. This is where manually scanning thread dumps with your eyes can be slow, so these tools can definitely save some time. I took some deadlock code I found online and ran it. Using "jstack", I captured a thread dump and used both tools to see how they report the deadlock. Here's what to expect if you analyze thread dumps that have deadlocks in them:
Samurai Table of Threads Showing The Two That Are Deadlocked |
Samurai Showing In Red The Stacktraces of the Deadlocked Threads |
TDA Showing The Deadlocked Threads and Their Stacktraces |
Thanks John... posting about thread dump analysis.
ReplyDeleteI have question ... is it possible to call above tools( Samurai & TDA ) from command line, if possible how I can call ?
This tool http://fastthread.io/ is faster and easier for analyzing thread dumps, which might actually be worth trying.
ReplyDelete+1 to http://fastthread.io. It provides nice reports. It would be nice if it offered an export feature for sharing.
Delete