在以前的文章中,咱們介紹了JDK14中jstat工具的使用,本文咱們再深刻探討一下jstack工具的使用。java
jstack工具主要用來打印java堆棧信息,主要是java的class名字,方法名,字節碼索引,行數等信息。程序員
更多精彩內容且看:編程
更多內容請訪問 www.flydean.com
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
jstack的參數比較簡單,l能夠包含鎖的信息,e包含了額外的信息。c#
咱們舉個例子:安全
jstack -l -e 53528
輸出結果以下:多線程
2020-05-09 21:46:51 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, 0x00007fda042be000 } "Reference Handler" #2 daemon prio=10 os_prio=31 cpu=0.67ms elapsed=66335.21s allocated=0B defined_classes=0 tid=0x00007fda04811000 nid=0x4603 waiting on condition [0x000070000afe1000] 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) Locked ownable synchronizers: - None ... "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 JNI global refs: 43, weak refs: 45
輸出的結果咱們能夠分爲下面幾個部分:jvm
JVM虛擬機信息編程語言
第一部分是JVM虛擬機的信息工具
2020-05-09 21:46:51 Full thread dump Java HotSpot(TM) 64-Bit Server VM (14.0.1+7 mixed mode, sharing):
上面顯示了虛擬機的thread dump時間和虛擬機的版本等信息。區塊鏈
Threads class SMR info
第二部分是JVM中非JVM(非VM和非GC的線程)的內部線程信息。
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 }
這些elements是和後面線程的tid相匹配的。表示的是本地線程對象的地址,注意這些不是線程的ID。
你們可能注意到了裏面寫的是SMR, SMR全稱是Safe Memory Reclamation。
什麼是SMR呢?簡單點講就是安全的內存分配,通常這個問題會出如今非自動GC的編程語言中如C++。在這些語言中,須要本身來爲對象分配內存和銷燬對象,這樣就可能致使在多線程的環境中,一個地址可能被分配給了多個對象,從而出現了內存分配的不安全。
線程信息
第三部分就是線程的具體信息了:
"Reference Handler" #2 daemon prio=10 os_prio=31 cpu=0.67ms elapsed=66335.21s allocated=0B defined_classes=0 tid=0x00007fda04811000 nid=0x4603 waiting on condition [0x000070000afe1000] 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) Locked ownable synchronizers: - None
按照字段的順序,咱們能夠把線程信息分爲下面幾個部分:
注意'allocated=' 和 'defined_classes=' 必需要開啓 -XX:+PrintExtendedThreadInfo纔會輸出數據。
Thread Stack Trace
接下來就是線程的堆棧信息:
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)
上面的例子是線程的堆棧信息,而且列出來了線程的狀態。
Locked Ownable Synchronizer
接下來的部分是該線程擁有的,可用的用於同步的排它鎖對象。
Ownable Synchronizer是一個同步器,這個同步器的同步屬性是經過使用AbstractOwnableSynchronizer或者它的子類來實現的。
例如ReentrantLock和ReentrantReadWriteLock中的write-lock(注意不是read-lock,由於須要排它性)就是兩個例子。
JVM Threads
接下來是JVM的線程信息,由於這個線程是JVM內部的,因此沒有線程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
JNI References
最後一部分是JNI(Java Native Interface)引用的信息,注意這些引用可能會致使內存泄露,由於這些native的引用並不會被自動垃圾回收。
JNI global refs: 43, weak refs: 45
jstack是分析線程的很是強大的工具,但願你們可以使用起來。
本文做者:flydean程序那些事本文連接:http://www.flydean.com/jdk14-jvm-jstack/
本文來源:flydean的博客
歡迎關注個人公衆號:程序那些事,更多精彩等着您!