Introduction to the

In the previous article, we introduced the use of the Jstat tool in JDK14. In this article, we take a closer look at the use of the JStack tool.

The jStack tool is mainly used to print the Java stack information, mainly the Java class name, method name, bytecode index, line number and other information.

More highlights:

  • Blockchain from getting started to Giving up series tutorials – with ongoing updates covering cryptography, Hyperledger, Ethereum,Libra, Bitcoin and more
  • Spring Boot 2.X Series tutorials: Learn Spring Boot from Scratch in seven days – continuous updates
  • Spring 5.X Series tutorials: Everything you can think of in Spring5 – constantly updated
  • Java Programmer from Handyman to Expert to God (2020 edition) – Ongoing updates with detailed articles and tutorials

For more, visit www.flydean.com

Jstack command format

Usage:
    jstack [-l][-e] <pid>
        (to connect to running process)

Options:
    -l  long listing. Prints additional information about locks
    -e  extended listing. Prints additional information about threads
    -? -h --help -help to print this help message
Copy the code

The arguments to jStack are simple; L can contain information about the lock, and E contains additional information.

The use of jstack

Let’s take an example:

jstack -l -e 53528
Copy the code

The following output is displayed:

Full Thread Dump Java HotSpot(TM) 64-bit Server VM (14.0.1+7 Mixed mode, Sharing): Threads class SMR info: _java_thread_list=0x00007fda0660eb00, length=14, elements={ 0x00007fda04811000, 0x00007fda05845800, 0x00007fda05012000, 0x00007fda05847800, 0x00007fda05843800, 0x00007fda05854800, 0x00007fda0481f000, 0x00007fda0481f800, 0x00007fda04018800, 0x00007fda041ff800, 0x00007fda05a28800, 0x00007fda05b1a800, 0x00007fda05b1d800, Elapsed = elapsed= 4elapsed = 4elapsed = 4elapsed = 4elapsed = 4elapsed = 4elapsed = 4elapsed = 4elapsed = 4elapsed defined_classes=0 tid=0x00007fda04811000 nid=0x4603 waiting on condition [0x000070000afe1000] java.lang.Thread.State: A RUNNABLE at Java. Lang. Ref. Reference. WaitForReferencePendingList ([email protected] / Native Method) at Java. Lang. Ref. Reference. ProcessPendingReferences ([email protected] / Reference. Java: 241) at Java. Lang. Ref. Reference $ReferenceHandler. Run ([email protected] / Reference. Java: 213) Locked ownable synchronizers: - None ... Elapsed = elapsed= elapsed= elapsed= elapsed= 0 TID =0x00007fda0506b000 nid=0x4803 Os_prio = 60 elapsed= elapsed= 60. Elapsed = 60. S tid=0x00007fda0502a800 nid=0x3203 runnable "GC Thread#1" os_prio= 60 elapsed= 60 Elapsed = elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED = ELAPSED Tid =0x00007fda05015000 nid=0x6203 runnable "GC Thread#3" os_prio=31 CPU = 14.75ms elapsed=66332.78s tid=0x00007fda05138800 Os_prio =31 CPU =0.25ms Elapsed =66335.23s TID =0x00007fda05031000 nID =0x3303 RUNnable "G1 Main Marker" OS_PRIO =31 CPU =0.25ms Elapsed =66335.23s TID =0x00007fda05031000 nID =0x3303 runnable Elapsed =66335.23s TID = 0x00007fda05031800nid =0x4b03 Runnable "G1 Refine#0" OS_PRIo = 70 Elapsed =66335.23s TID = 0x00007fda05031800nid =0x4b03 Runnable "G1 Refine#0" Os_prio = 60 Elapsed = elapsed= 60. Elapsed = 60. S TID = 0x00007fDA0583a800 nID = 0x4A03 Elapsed = elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed Elapsed = elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= elapsed= tid= 0x00007fDA0505b000 nID = 0xA103 Waiting on condition JNI global refs: 60, weak refs: 45Copy the code

The output results can be divided into the following parts:

JVM VM information

The first part is the INFORMATION about the JVM virtual machine

2020-05-09 21:46:51
Full thread dump Java HotSpot(TM) 64-Bit Server VM (14.01.+7 mixed mode, sharing):
Copy the code

The thread dump time and VM version information are displayed.

Threads class SMR info

The second part is the internal thread information of the JVM for non-Jvms (non-VM and non-GC threads).

Threads class SMR info:
_java_thread_list=0x00007fda0660eb00, length=14, elements={
0x00007fda04811000, 0x00007fda05845800, 0x00007fda05012000, 0x00007fda05847800,
0x00007fda05843800, 0x00007fda05854800, 0x00007fda0481f000, 0x00007fda0481f800,
0x00007fda04018800, 0x00007fda041ff800, 0x00007fda05a28800, 0x00007fda05b1a800,
0x00007fda05b1d800, 0x00007fda042be000
}
Copy the code

These elements match the tiDs of subsequent threads. Represents the address of the local thread object, note that these are not thread ids.

You may have noticed that SMR stands for Safe Memory Reclamation.

What is SMR? In a nutshell, this is safe memory allocation, which is a common problem in non-automatic GC programming languages such as C++. In these languages, it is necessary to allocate memory for objects and destroy objects itself, which can lead to an unsafe memory allocation in a multi-threaded environment where one address can be assigned to multiple objects.

Thread information

The third part is the details of the thread:

Elapsed =66335.21s allocated=0B defined_classes=0 "Reference Handler" #2 Elapsed =10 OS_PRIo =31 CPU =0.67ms Elapsed =66335.21s defined_classes=0 tid=0x00007fda04811000 nid=0x4603 waiting on condition [0x000070000afe1000] java.lang.Thread.State: A RUNNABLE at Java. Lang. Ref. Reference. WaitForReferencePendingList ([email protected] / Native Method) at Java. Lang. Ref. Reference. ProcessPendingReferences ([email protected] / Reference. Java: 241) at Java. Lang. Ref. Reference $ReferenceHandler. Run ([email protected] / Reference. Java: 213) Locked ownable synchronizers: - NoneCopy the code

Thread information can be divided into the following sections, in the order of fields:

  • Thread name: For example, Reference Handler
  • Thread ID: for example #2
  • Daemon Threads: For example, Daemon Threads are low-priority threads that serve User threads. Because daemon Threads are of low priority and only serve user threads, the JVM automatically exits when all user threads are finished, regardless of whether any Daemon Threads are still running.
  • Priority: For example, prio=10
  • OS thread priority: For example, OS_PRIO =31
  • CPU time: the CPU acquisition time of the thread, for example, CPU =0.67ms
  • Elapsed: The wall clock time elapsed after the thread has started
  • Allocated: Number of bytes allocated by the thread
  • Defined_classes: The number of classes defined by this thread

Note that ‘allocated=’ and ‘defined_classes=’ must be enabled -xx :+PrintExtendedThreadInfo to export data.

  • Address: indicates the Address of the Java thread, for example, tid=0x00007fda04811000
  • OS thread ID: For example, nID =0x4603
  • Thread state: for example, waiting on condition
  • Latest Java stack pointer: latest Java stack pointer SP, for example :[0x000070000AFE1000]

Thread Stack Trace

Here is the thread stack:

java.lang.Thread.State: RUNNABLE
	at java.lang.ref.Reference.waitForReferencePendingList(java.base@14.0.1/Native Method)
	at java.lang.ref.Reference.processPendingReferences(java.base@14.0.1/Reference.java:241)
	at java.lang.ref.Reference$ReferenceHandler.run(java.base@14.0.1/Reference.java:213)
Copy the code

The above example is the thread stack and lists the thread state.

Locked Ownable Synchronizer

The next part is the exclusive lock object that the thread owns and can use for synchronization.

Ownable Synchronizer is a Synchronizer, the Synchronizer synchronization property is through the use of AbstractOwnableSynchronizer or its subclasses.

Examples are ReentrantLock and write-lock in ReentrantReadWriteLock (note not read-lock because exclusivity is required).

JVM Threads

Next is the thread information for the JVM. Since this thread is internal to the JVM, there is no thread ID:

"VM Thread" os_prio=31 cpu=1433.78ms elapsed=66335.22s tid=0x00007fda0506b000 nid=0x4803 runnable

"GC Thread#0" os_prio=31 cpu=18.63ms elapsed=66335.23s tid=0x00007fda0502a800 nid=0x3203 runnable

"GC Thread#1" os_prio=31 cpu=19.64ms elapsed=66334.06s tid=0x00007fda050e5800 nid=0x9d03 runnable

"GC Thread#2" os_prio=31 cpu=17.72ms elapsed=66334.06s tid=0x00007fda05015000 nid=0x6203 runnable

"GC Thread#3" os_prio=31 cpu=14.57ms elapsed=66332.78s tid=0x00007fda05138800 nid=0x6503 runnable

"G1 Main Marker" os_prio=31 cpu=0.25ms elapsed=66335.23s tid=0x00007fda05031000 nid=0x3303 runnable

"G1 Conc#0" os_prio=31 cpu=14.85ms elapsed=66335.23s tid=0x00007fda05031800 nid=0x4b03 runnable

"G1 Refine#0" os_prio=31 cpu=3.25ms elapsed=66335.23s tid=0x00007fda0583a800 nid=0x4a03 runnable

"G1 Young RemSet Sampling" os_prio=31 cpu=5929.79ms elapsed=66335.23s tid=0x00007fda0505a800 nid=0x3503 runnable
"VM Periodic Task Thread" os_prio=31 cpu=21862.12ms elapsed=66335.13s tid=0x00007fda0505b000 nid=0xa103 waiting on condition
Copy the code

JNI References

The last part is information about JNI (Java Native Interface) references. Note that these references may cause memory leaks because Native references are not automatically garbage collected.

JNI global refs: 43, weak refs: 45
Copy the code

conclusion

Jstack is a very powerful tool for analyzing threads, and I hope you can use it.

Author: Flydean program stuff

Link to this article: www.flydean.com/jdk14-jvm-j…

Source: Flydean’s blog

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!