Java’s own tool jstack intercepts the stack information in the process

In the process of using java software, strange problems sometimes appear inexplicably. These problems are often unable to be located using log information. At this time, we need to analyze the problem by looking at the stack call relationship of threads in the process.

For example, when we are doing an operation, many warning boxes will pop up inexplicably, some of which are normal and some are not. For these error warning messages, how can we locate where the error pop-up box appears in the code? After the pop-up, we need to check each thread of the software to find out which thread caused the problem. However, sometimes because of environment, time and other problems, we can't debug with the IDE at all. We can only take a memory snapshot through the tool software, and then analyze the memory information.

Today we introduce a common tool: jstack

Jstack is the JDK's own tool, and it is also a software with a very high appearance rate in JVM performance tuning. So it is very necessary to master it.

Jstack can generate a thread snapshot of the JVM at the current point in time.

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

1. Locate the causes of long pause of threads through thread snapshot, such as deadlock between threads, dead loop, and long wait caused by requesting external resources

2. Analyze the calling relationship of the currently executing method through thread snapshot to determine the source of exception information.

Its use is very simple:

(PS: the premise is that you have installed JDK with jstack. At the same time, you'd better have set the environment variable.)

Step 1: view the PID of the process through the task manager of windows

Here's what PID is: PID is the ID of each process. It is the only ID assigned by the operating system to identify the process identity after the software is started

As shown in the figure

Under the process tab, View > select columns

Tick PID and confirm

Switch to the application tab and select the program to snapshot memory. Android studio is selected in the picture. Right click to go to process.

Here you can see that the PID corresponding to Android studio is 9952

Step 2 open the command line and execute the jstack program

Note that if the environment variable is not successfully added, it can only be executed under the path of jstock, otherwise the operating system cannot recognize it.

As shown in the figure, there are generally two operating parameters used to take memory snapshots,

Their meanings are as follows:

-L long listings, additional lock information will be printed. When a life and death lock occurs, you can use jstack - L PID to observe the lock holding

-M mixed mode will output not only Java stack information, but also C / C + + stack information (such as native method)

We usually use the - L parameter to meet our needs

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

PS note here that > > means redirection, that is, directionally output the captured snapshot to 987 Txt. > > You'd better keep a space twice

In this way, we will generate a 987 Txt file, and write the memory snapshot to this text at the same time

As shown below:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>