Detailed explanation of the use of Java's jstack command

Introduction to the jstack command

jstack (Java Virtual Machine Stack Trace) is a command-line tool provided by JDK that can generate thread snapshot information of the current moment of the Java virtual machine. A thread snapshot, commonly referred to as a threaddump or javacore file, is a collection of the currently executing Java threads, virtual machine internal threads, and optional native method stack frames for each thread in the Java virtual machine. For each method stack frame, the full class name, method name, bytecode index (BCI) and line number are displayed. The generated thread snapshot can be used to locate the reasons for the long pause of the thread, such as: deadlock between threads, infinite loop, requesting external resources to be suspended for a long time, etc.

jstack command parameters

Command syntax:

jstack [options] pid

Command parameter description:

  • option: Optional argument to the jstack command. If this parameter is not specified, the jstack command will display the thread snapshot information of the Java virtual machine at the current moment, as shown in the following figure:

Manneko Gakusha.png

  • pid: The process ID of the Java virtual machine for which configuration information is to be printed.

To get a list of running Java virtual machine processes, use the ps command (on Linux systems) or the tasklist command (on Windows systems), or if the Java virtual machine processes are not running in a separate docker instance, use the jps command.

optionWhat are the parameters? Let's take a look.

-F parameter

If the Java Virtual Machine process becomes unresponsive because the process hangs, a -Fparameter (only available on Oracle Solaris and Linux operating systems) can be used to force thread snapshot information to be displayed.

for example:

Manneko Gakusha.png

-l parameter

If -lparameters are used, in addition to the method stack frame, the jstack command displays additional information about the lock, such as a list of ownable synchronizers belonging to java.util.concurrent.

for example:

Manneko Gakusha.png

-m parameter

If used with -mparameters, the jstack command will display mixed stack frame information, in addition to Java method stack frames, there are also native method stack frames. Native method stack frames are virtual machine code or JNI/native code written in C or C++.

for example:

Manneko Gakusha.png

In the displayed results, frames prefixed with an asterisk are Java method stack frames, while those not prefixed with an asterisk are native method stack frames. for example:

Manneko Gakusha.png

-h and -help

Displays help information for the jstack command.

end

Although the jstack command has been introduced for a long time and is used frequently, it is still an "experimental and unsupported" (Experimental and Unsupported) tool, which may be fixed in the future, or may be silent in a certain JDK version disappeared without a trace. So, use it and cherish it.

However, we can also use the Thread.getAllStackTracesgetAll() method to obtain the StackTraceElementobjects of all threads in the Java virtual machine, and then obtain all thread stack frame information.

Finally, thank you for being so handsome, and for giving me likes and attention .

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324077911&siteId=291194637