Thread dumps are vital artifacts to debug & troubleshoot production performance problems. Thread dump files tend to span for several hundreds of lines (sometimes a few thousands of lines). It is hard to conceive and assimilate all the information in the thread dump due to its verbosity. If information present in the thread dumps can be condensed into one compact flame graph, it would make our analysis job much easier.

In this article, let us review how to generate a Flame graph from your Java application. Using the steps outlined in this article, you can not only generate flame graphs from Java applications but also from any programming language running on JVM such as Java, Scala, Jython, kotlin, jruby,…

How to generate Flame Graph?
There are two simple steps to generate a flame graph from your application:

1. Capture thread dumps
2. Analyze with fastThread tool

Let us discuss these steps in detail.

1. Capture thread dumps:
Capture the thread dumps when a performance problem is brewing in the application. There are 8 different options to capture thread dumps. You can use the option that best suits your environment.

My favorite option is ‘jstack’. ‘jstack’ is an effective command line tool to capture thread dumps. jstack tool is shipped in JDK_HOME/bin folder. Here is the command that you need to issue to capture thread dump:
jstack -l <pid> > <file-path>

where

pid: is the Process Id of the application, whose thread dump should be captured

file-path: is the file path where thread dump will be written into.

Example:

jstack -l 37320 > /opt/tmp/threadDump.txt

As per the example thread dump of the process would be generated in /opt/tmp/threadDump.txt file.

2. Analyze with fastThread tool:
Once thread dump is captured, upload the generated thread dump to fastThread tool. fastThread is a free online thread dump analysis tool that will analyze your thread dump and generate a beautiful analysis report instantly. Here is the sample report generated by fastThread. At the bottom of this report, you will be able to see Flame Graph.

Note: if you are not comfortable in uploading your thread dump to the fastThread tool, which is running on the cloud, you can register here to download and install the tool locally on your machine and then do the analysis.


Fig: Flame Graph generated by fastThread

How to use Flame Graph?
From the Flame Graph, you will be able to see lines of code that your application is executing. The number of threads that are executing those lines of code. You can also observe most executed 3rd party libraries, frameworks. The flame graph provides you zoom a tower in the flame graph. It also gives you the capability to search method, class or package name in your application. For more details on how to use this Flame Graph effectively, you can watch the below video clip.





Conclusion
Flame graphs give a compact view of your thread dumps. Instead of going through hundreds of lines of stack traces in the thread dump, it would be a lot easier to visualize information in the graphical format.