In Windows, Jstack is used to intercept the stack information in the Java Process.

Source: Internet
Author: User

In Windows, Jstack is used to intercept the stack information in the Java Process.

During the use of Java software, strange problems may sometimes occur. These problems are often unable to be identified using log information. In this case, we need to check the stack call relationship of threads in the process to analyze the problem.

For example, when we perform an operation, multiple warning boxes are displayed, some of which are normal and some are not. For the warning information of these errors, how do we locate the position where the code has an error pop-up box? We need to check the various threads of the software after the bullet box to find out which thread caused the problem. However, sometimes due to environment, time, and other problems, we simply cannot debug with IDE (you cannot always take a notebook to the client and say, buddy, I will debug it (anti-theft connection: this article first from the http://www.cnblogs.com/jilodream/), can only take a memory snapshot through the tool software, and then analyze the memory information.

Today we will introduce a common tool: Jstack

Jstack is a built-in JDK tool and a software with a very high output rate in JVM performance tuning. Therefore, it is necessary to master it.

Jstack can generate a thread snapshot for the current JVM time point.

A thread snapshot is a collection of method stacks that are being executed by each thread in the current JVM. The main reason for generating a thread snapshot is:

1. Locate the cause of long pause in the thread through the thread snapshot, such as the deadlock between threads, the endless loop, and the long wait caused by requests to external resources.

2. Analyze the call relationship of the current execution method through the thread snapshot to determine the source of the exception information.

 

It is easy to use:

(Ps: The premise is that you have installed JDK with Jstack. At the same time, it is recommended that you have set the environment variable .)

Step 1: view the PID of a process in the Windows Task Manager

What is PID: PID is the identity of each process. It is a unique identifier assigned by the operating system to identify the process after the software is started.

On the process tab, click View> Select column.

Select the PID and then confirm

Switch to the Application tab and select the program for which the snapshot memory is to be created. Android Studio is selected in the image. Right-click to go to the process.

 

Here, we can see that the PID of Android Studio is 9952.

 

Step 2 open the command line and execute the Jstack Program

Note: If the environment variable is not successfully added, it can only be executed in the Jstack path. Otherwise, the operating system cannot identify it.

Generally, there are two running parameters used to take a memory snapshot,

Their meanings are as follows:

-L long listings, will print out the amount (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream/) of the lock information, in the occurrence of a deadlock can be used jstack-l pid to observe the lock hold

-M mixed mode: not only outputs Java stack information, but also outputs C/C ++ stack information (such as Native method)

 

We generally use the-l parameter to meet the needs.

The format is as follows: Jstack-l PID> 123.txt

Ps pay attention to the following >> this is the idea of focusing on, and it also refers to outputting the captured fast-directed Image to 987.txt.> It is best to keep spaces for two times.

In this case, we will generate a 987.txt file under the command line, and write the memory snapshot into this text.

For example:

 

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.