Summary of how Thread dumps is crawled in Java _java

Source: Internet
Author: User
Tags time interval

Thread dumps can help us determine CPU peaks, deadlocks, memory exceptions, application insensitivity, response time, and other system problems. Some online analytics tools like http://fastthread.io/can also help us analyze and locate problems, but these tools require a dump file. So in this article, I've summarized the way to crawl Java Thread dumps files in 7.

1. Jstack

Jstack is a valid command-line tool for capturing the thread dump file, located under the Bin folder (Jdk_home\bin) in the JDK directory, and the following is the command to crawl the dump file:

Jstack-l <pid> > <file-path>

Description

Pid:java application process ID, which 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 the use of Jstack to generate dump files into the/opt/tmp/threaddump.txt directory.

Starting with Java5, Jstack is included in the JDK, and if you use an older version of the JDK, consider using a different approach.

2. Kill-3

In the context of security considerations, a part of the production environment contains only the JRE environment, so the Jstack tool cannot be used, in which case we can use the Kill-3 method:

kill -3 <pid>

Description

Pid:java application process ID, which is the application process ID that needs to crawl the dump file.
Example:

kill -3 37320
When a dump file is generated using kill-3, the dump file is 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

The 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, in the left panel (shown below), lists the JVM information that is running, which can crawl the dump file from a local or remotely running JVM.

Click on the Thread Dump button for the process name above to generate the dump file, as shown in the following figure:

4. JMC

Java Mission Control (JMC) is a tool that collects and analyzes data from a local or production environment and, starting with Oracle JDK 7 Update 40, is included in the JDK, which generates dump files from the JVM. JMC is located in the Jdk_home\bin\jmc.exe file:

After you run the tool, you can see the Java process running locally, which can also be connected to a remote machine. Double-click the Java process where you want to generate the dump file, and click Flight Recorder, and you will see the following dialog box:

In the Thread dump dropdown box, you can choose the time interval to generate the dump file. In the example above, a dump file will be generated every 60 seconds. After the selection is complete, start Flight recorder, and you can see the contents of the dump file in the Threads panel:

5. Windows (Ctrl + Break)

This approach is only valid on the Windows operating system:

Select the command line on the console window

Press Ctrl + BREAK command on the command line window

The dump file is then generated and the contents of the dump file are printed on the command Line window.

Note 1: There are several notebooks (such as the Lenovo T series) have canceled the "break" button, in this case you have to use Google search and break key function similar to the key, I found that "function key + B" key and break key function of the same, so I used "Ctr L + Fn + B "key to generate dump file.

Note 2: There is a drawback to the above method is that the contents of the dump file will be printed to the console, without the dump file, we can not use analysis tools such as Http://fasthread.io to analyze the dump file. So you can use the following command to output the contents of the dump file to a text file, such as your application name is Samplethreadprogram, then the commonly used 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
When you press the CTRL + Break button, the dump file is saved to the C:\workspace\threadDump.txt.

6. Threadmxbean

Starting with JDK 1.5, Threadmxbean was introduced. This is the JVM's management interface, with which you need only a small amount of code to generate the dump file, the following is the main implementation of the dump file using Threadmxbean:

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 utility monitoring tools provide the ability to generate dump files, and if you use APP Dynamics to monitor your application, here are the steps to generate the dump file:

Open the Create Action window and select Diagnostics->take a thread dump in the Create Action window;

Enter the name of the action, the number of fetching dump files, and the time interval for fetching the dump file (milliseconds);

If you want to do something before the grab dump action starts, 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

While I've listed 7 ways to crawl the dump file, I'm blunt that jstack and kill-3 are the best choices for the following reasons:

A. Simple, easy to achieve;

B. Common: In most cases, regardless of operating system type, Java vendor, JVM version, and so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.