In the process of using Java software, strange problems sometimes come to the unknown. These problems often do not use log information to locate, then we need to look at the process internal thread's stack call relationship to analyze where the problem is.
For example, when we do an operation, there will be a number of unknown pop-up warning boxes, some of which are normal, some are not. For these error warning messages, how do we locate the code where the error popped up the box? We need to look at the various threads of the software after the frame, to find out which thread is causing the problem. But sometimes because of the environment, time and other problems, we can not take the IDE to debug, only through the tool software to take snapshots of memory, and then analyze memory information.
Today, we introduce a common tool:jstack
The Jstack is the JDK's own tool, and it is a software that has a very high raspberry performance in the JVM. So it is very necessary to master it.
Jstack can generate a snapshot of the threads at the current point-in-time of the JVM.
A thread snapshot is a collection of method stacks in which each thread in the current JVM is executing. The main reason for generating a thread snapshot:
1, through the thread snapshot to locate a thread for a long pause, such as deadlock between threads, dead loops, request external resources caused by a long time to wait
2. Analyze the call relationship of the current execution method through a thread snapshot to determine the source of the exception information.
It's very simple to use:
PS: If you already have a JDK with jstack. It is also best to set up environment variables. )
Step one: View the PID of a process through Windows Task Manager
Here is a simple saying what is pid:pid is the identity of the process, he is the software started, the operating system allocated by the unique identity of the identity of the process to identify
As shown in figure
In the Process page check, view > select columns
Check the PID and make sure
To the Application page check, select the program to which you want to snapshot memory. Android Studio is selected in the picture. Right-click to go to process.
So here's a look at the Android studio PID is 9952.
The second step opens the command line and executes the Jstack program
Note that if you do not add the environment variables successfully, this can only be done under the Jstack path, otherwise the operating system will not be recognized.
As shown here, there are generally two running parameters for taking snapshots of memory,
Their meaning is as follows:
-L long listings, will print out additional lock information, in the event of deadlock can use jstack-l PID to observe the lock hold
-M mixed mode, which not only outputs Java stack information, but also outputs C + + stack information (such as the native method)
We generally use the-l parameter to meet our needs
The format is as follows Jstack-l PID >> 123.txt
PS Here Note that >> is the meaning of redirection, that is, the snapshot of the snap to the output to 987.txt. >> two times best to keep space
This will generate a 987.txt file under the command-line path and write the memory snapshot into this text
The following figure: