0 概述
jstack 指令用于生成虛拟機目前時刻線程快照(一般稱為threaddump 或者javacore 檔案)。線程快照就是目前虛拟機内每一條線程正在執行的方法堆棧集合,生成線程快照的目的主要用于定位如線程間的死鎖、死循環、請求外部服務時間過長等導緻機器load、cpu等過高。本文主要結合top、jstack指令來實際分析cpu過高線程。
1 jstack指令
jstack [option] pid
option | 作用 |
---|---|
-F | 當正常輸出請求不被響應時候,強制輸出線程堆棧 |
-l | 顯示鎖相關資訊 |
-m | 混合模式,如果調用到本地方法棧,會顯示本地方法棧 |
2 執行個體分析
public class ThreadTest {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//死循環
while (true) {
}
}
});
thread.start();
//主線程不退出
Thread.sleep();
}
}
啟動程式後,
1.通過jps -l 找到相應java程序,
2.然後使用top -Hp pid (可以列印出改程序中線程運作情況)從圖中可以看出20018 (16進制:4e32)比較消耗cpu
3.執行jstack pid (實際工作中可能吧比較多,可以将其輸出到檔案中)
jstack
-- ::
Full thread dump Java HotSpot(TM) -Bit Server VM (-b15 mixed mode):
"Attach Listener" #10 daemon prio=9 os_prio=0 tid=0x00007f3870001000 nid=0x4f3c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Thread-0" #9 prio=5 os_prio=0 tid=0x00007f38b00e4800 nid=0x4e32 runnable [0x00007f3892eab000]
java.lang.Thread.State: RUNNABLE
at ThreadTest$1.run(ThreadTest.java:)
at java.lang.Thread.run(Thread.java:)
"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007f38b00c7000 nid=0x4e30 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007f38b00bc000 nid=0x4e2f waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f38b00ba000 nid=0x4e2e waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f38b00b7000 nid=0x4e2d waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f38b00b5800 nid=0x4e2c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f38b0083000 nid=0x4e2b in Object.wait() [0x00007f38935b2000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:)
- locked <> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:)
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f38b007e000 nid=0x4e2a in Object.wait() [0x00007f38936b3000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:)
at java.lang.ref.Reference.tryHandlePending(Reference.java:)
- locked <> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:)
"main" #1 prio=5 os_prio=0 tid=0x00007f38b0009000 nid=0x4e24 waiting on condition [0x00007f38b5e12000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at ThreadTest.main(ThreadTest.java:)
"VM Thread" os_prio= tid= nid= runnable
"GC task thread#0 (ParallelGC)" os_prio= tid= nid= runnable
"GC task thread#1 (ParallelGC)" os_prio= tid= nid= runnable
"GC task thread#2 (ParallelGC)" os_prio= tid= nid= runnable
"GC task thread#3 (ParallelGC)" os_prio= tid= nid= runnable
"VM Periodic Task Thread" os_prio= tid= nid= waiting on condition
JNI global references:
搜尋對應線程Id(4e32),就可以找到對應代碼區域
"Thread-0" #9 prio=5 os_prio=0 tid=0x00007f38b00e4800 nid=0x4e32 runnable [0x00007f3892eab000]
java.lang.Thread.State: RUNNABLE
at ThreadTest$1.run(ThreadTest.java:)
at java.lang.Thread.run(Thread.java:)
參考文獻:
[1] 深入了解java 虛拟機(第二版),周志明著