Thread dumps can help us determine CPU peaks, deadlocks, memory exceptions, slow application response, long response time and other system problems. Some online analysis tools such as http://fastthread.io/ can also help us analyze and locate problems, but these tools require a dump file. Therefore, in this article, I summarized the ways to grab Java Thread Dumps files in 7.
1. jstack
jstack is an effective command-line tool for crawling thread dump files. It is located in the bin folder (JDK_HOME/bin) in the JDK directory. The following is the command to crawl dump files:
jstack -l <pid> > <file-path>
illustrate:
pid: The process id of the Java application, that is, the application process id that needs to crawl the dump file.
file-path: The path to save the dump file.
Example:
jstack -l 37320 > /opt/tmp/threadDump.txt
The above example demonstrates using jstack to generate dump files into the /opt/tmp/threadDump.txt directory.
Starting from Java5, jstack has been included in jdk. If you use the old version of jdk, you should consider using other methods.
2. Kill -3
In terms of security considerations, some machines in production environments only contain JRE environments, so the jstack tool cannot be used. In this case, we can use kill -3:
kill -3 <pid>
illustrate:
pid: The process id of the Java application, that is, the application process id that needs to crawl the dump file.
Example:
kill -3 37320
When using kill -3 to generate dump files, the dump files are output to the standard error stream. If your application is running on tomcat, the dump content will be sent to the <TOMCAT_HOME>/logs/catalina.out file.
3. JVisualVM
Java VisualVM is a graphical interface tool that can provide JVM information. It is located in the JDK_HOME/bin/jvisualvm.exe file. Starting with JDK6 Update7, it is included in the JDK.
Run jvisualvm, and in the left panel (as shown in the figure below), the running JVM information is listed. This tool can grab dump files from local or remote running JVMs.
Click the Thread Dump button corresponding to the process name in the figure above to generate a dump file, as shown in the figure below:
4. JMC
Java Mission Control (JMC) is a tool that can collect and analyze data from a local or production environment. Starting with Oracle JDK 7 Update 40, it is included in the JDK, which can generate dump files from the JVM. JMC is located in the JDK_HOME/bin/jmc.exe file:
After running the tool, you can see the Java process running locally, which can also be connected to a remote machine. Double-click the Java process you want to generate the dump file, click Flight Recorder, and you will see the following dialog box:
In the Thread Dump drop-down box, you can select the time interval for generating the dump file. In the example above, a dump file will be generated every 60 seconds. After selecting complete, start Flight recorder and you can see the contents of the dump file in the Threads panel:
5. Windows (Ctrl + Break)
This method is only valid on Windows operating systems:
Select the command line on the console window
Press the "Ctrl + Break" command on the command line window
Then a dump file will be generated, and the contents of the dump file will be printed on the command line window.
Note 1: There are several notebooks (such as the Lenovo T series) that have cancelled the "Break" key, in which case you have to google for keys that have similar functions as the Break key, and I found that the "Function key + B" key is the same as the Break key, so I used the "Ctrl + Fn + B" key to generate the dump file.
Note 2: One disadvantage of using the above method is that the content of the dump file will be printed to the console. If there is no dump file, it is difficult for us to use analysis tools such as http://fasthread.io to analyze dump file. Therefore, you can use the following command to output the content of the dump file to a text file. For example, your application name is SampleThreadProgram, so the usual commands are as follows:
java -classpath . SampleThreadProgram
The command to output the contents of the dump file to a text file is as follows:
java -classpath . SampleThreadProgram > C:/workspace/threadDump.txt 2>&1
After you press the "Ctrl + Break" key, the dump file will be saved to C:/workspace/threadDump.txt.
6. ThreadMXBean
Starting with JDK 1.5, ThreadMXBean has been introduced. This is the management interface of the JVM. Using this interface, you only need a small amount of code to generate dump files. The following is the main implementation of using ThreadMXBean to generate dump files:
public void dumpThreadDump() { ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean(); for (ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) { System.out.print(ti.toString()); }}7. APM Tool App Dynamics
Some application performance monitoring tools provide the function of generating dump files. If you use App Dynamics to monitor your application, the following are the steps to generate dump files:
Open the Create Action window and select Diagnostics->Take a thread dump in the Create Action window;
Enter the action name, the number of dump files to crawl, and the time interval (milliseconds) for crawling dump files;
If you want to perform some actions before the crawling dump action begins, you can select the Require approval executing before this Action check box and enter the email address of the individual or group;
Click OK.
Summarize
Although I listed 7 ways to grab dump files earlier, IMHO, jstack and kill -3 are the best choices for the following reasons:
a. Simple, easy to implement;
b. General: In most cases, regardless of operating system type, Java vendor, JVM version, etc.