JVM监控及诊断工具-命令之jstack
Posted 爱上口袋的天空
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JVM监控及诊断工具-命令之jstack相关的知识,希望对你有一定的参考价值。
一、简介
jstack( JVM Stack Trace):用于生成虚拟机指定进程当前时刻 的线程快照(虚拟机堆栈跟踪 )。线程快照就是当前虚拟机内指定进程的每一条线程正在执行的 方法堆栈的集合
生成线程快照的作用:可用于定位线程出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致的长时间等待等问题。这些都是导致线程长时间停顿的常见原因。当线程出现停顿时,就可以用jstack显示各个线程调用的堆栈情况。
在thread dump中,要留意下面几种状态死锁,,Deadlock(重点关注)等待资源, Waiting on condition(重点关注)等待获取监视器, Waiting on monitor entry(重点关注)阻塞, Blocked(重点关注)执行中, Runnable暂停, Suspended
二、创建一个死锁的程序代码,使用jstack命令查看
1、程序代码
package com.kgf.kgfjavalearning2021.jvm; /*** * 测试线程间的死锁问题 */ public class JstackLockTest public static void main(String[] args) StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); new Thread(new Runnable() @Override public void run() synchronized (sb1) sb1.append("a"); sb2.append("1"); try Thread.sleep(100); catch (InterruptedException e) e.printStackTrace(); synchronized (sb2) sb1.append("b"); sb2.append("2"); try Thread.sleep(100); catch (InterruptedException e) e.printStackTrace(); ).start(); new Thread(new Runnable() @Override public void run() synchronized (sb2) sb1.append("c"); sb2.append("3"); try Thread.sleep(100); catch (InterruptedException e) e.printStackTrace(); synchronized (sb1) sb1.append("d"); sb2.append("4"); try Thread.sleep(100); catch (InterruptedException e) e.printStackTrace(); ).start();
2、启动程序后使用jstack命令分析
命令:jstack pid
查看输出文件的内容:
2021-11-30 22:24:12 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.51-b03 mixed mode): "DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x000000001e712000 nid=0x3b14 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Thread-1" #13 prio=5 os_prio=0 tid=0x000000001e719000 nid=0x2e50 waiting for monitor entry [0x0000000020d5f000] java.lang.Thread.State: BLOCKED (on object monitor) at com.kgf.kgfjavalearning2021.jvm.JstackLockTest$2.run(JstackLockTest.java:50) - waiting to lock <0x000000076b92c860> (a java.lang.StringBuilder) - locked <0x000000076b92c8a8> (a java.lang.StringBuilder) at java.lang.Thread.run(Thread.java:745) "Thread-0" #12 prio=5 os_prio=0 tid=0x000000001e715000 nid=0x2024 waiting for monitor entry [0x0000000020c5e000] java.lang.Thread.State: BLOCKED (on object monitor) at com.kgf.kgfjavalearning2021.jvm.JstackLockTest$1.run(JstackLockTest.java:26) - waiting to lock <0x000000076b92c8a8> (a java.lang.StringBuilder) - locked <0x000000076b92c860> (a java.lang.StringBuilder) at java.lang.Thread.run(Thread.java:745) "Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001e718000 nid=0x29f4 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001fcdf800 nid=0x23d4 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001fcdf000 nid=0x13b4 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001fcde000 nid=0x19b0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001fcd9800 nid=0x1ec4 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001e716000 nid=0x1588 runnable [0x000000002055e000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:170) at java.net.SocketInputStream.read(SocketInputStream.java:141) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) - locked <0x000000076b98f038> (a java.io.InputStreamReader) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:161) at java.io.BufferedReader.readLine(BufferedReader.java:324) - locked <0x000000076b98f038> (a java.io.InputStreamReader) at java.io.BufferedReader.readLine(BufferedReader.java:389) at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64) "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e716800 nid=0x33a0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e714800 nid=0xffc runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001d00e000 nid=0x3590 in Object.wait() [0x000000001fa5f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b586f58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) - locked <0x000000076b586f58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209) "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001d00c800 nid=0x1180 in Object.wait() [0x000000001f95f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b586998> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157) - locked <0x000000076b586998> (a java.lang.ref.Reference$Lock) "VM Thread" os_prio=2 tid=0x000000001e6d2800 nid=0x2384 runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000003339800 nid=0x2a20 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000000000333b000 nid=0x754 runnable "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x000000000333d000 nid=0x3240 runnable "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x000000000333e800 nid=0x3110 runnable "GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000003340800 nid=0x3460 runnable "GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000003342000 nid=0x36d4 runnable "GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000003346000 nid=0x29a8 runnable "GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000003347000 nid=0x1c80 runnable "GC task thread#8 (ParallelGC)" os_prio=0 tid=0x0000000003348800 nid=0x1e54 runnable "GC task thread#9 (ParallelGC)" os_prio=0 tid=0x0000000003349800 nid=0x2460 runnable "VM Periodic Task Thread" os_prio=2 tid=0x000000001e9e5000 nid=0x1878 waiting on condition JNI global references: 33 Found one Java-level deadlock: ============================= "Thread-1": waiting to lock monitor 0x000000000341e668 (object 0x000000076b92c860, a java.lang.StringBuilder), which is held by "Thread-0" "Thread-0": waiting to lock monitor 0x000000000341d278 (object 0x000000076b92c8a8, a java.lang.StringBuilder), which is held by "Thread-1" Java stack information for the threads listed above: =================================================== "Thread-1": at com.kgf.kgfjavalearning2021.jvm.JstackLockTest$2.run(JstackLockTest.java:50) - waiting to lock <0x000000076b92c860> (a java.lang.StringBuilder) - locked <0x000000076b92c8a8> (a java.lang.StringBuilder) at java.lang.Thread.run(Thread.java:745) "Thread-0": at com.kgf.kgfjavalearning2021.jvm.JstackLockTest$1.run(JstackLockTest.java:26) - waiting to lock <0x000000076b92c8a8> (a java.lang.StringBuilder) - locked <0x000000076b92c860> (a java.lang.StringBuilder) at java.lang.Thread.run(Thread.java:745) Found 1 deadlock.
由上面的内容我们可以发现有两个线程处于阻塞状态:
可以发现上面线程的状态如下:
Thread-1:
waiting to lock <0x000000076b92c860> (a java.lang.StringBuilder)
locked <0x000000076b92c8a8> (a java.lang.StringBuilder)
Thread-0:
waiting to lock <0x000000076b92c8a8> (a java.lang.StringBuilder)
locked <0x000000076b92c860> (a java.lang.StringBuilder)因此上面的两个线程构成死锁
并且在我们的输出文件中也提示了死锁:
三、创建一个睡眠的程序代码,使用jstack命令查看
1、程序代码
package com.kgf.kgfjavalearning2021.jvm; /*** * 测试线程的睡眠问题 */ public class JstackSleepTest public static void main(String[] args) System.out.println("hello-1"); try Thread.sleep(1000*60*10); catch (InterruptedException e) e.printStackTrace(); System.out.println("hello-2");
上面的程序是一个睡眠程序,是有时间限制的,意思是睡眠10分钟后继续执行
2、启动程序后使用jstack命令分析
命令:jstack pid
输出文件中的内容:
2021-11-30 22:40:01 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.51-b03 mixed mode): "Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001fa51000 nid=0x3abc runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001f9d6000 nid=0x3aa0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001f9d5000 nid=0xa18 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001f9d4800 nid=0x1e18 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001f9d1800 nid=0x39cc waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001f9a0800 nid=0x379c runnable [0x000000002025e000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:170) at java.net.SocketInputStream.read(SocketInputStream.java:141) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) - locked <0x000000076b98ef30> (a java.io.InputStreamReader) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:161) at java.io.BufferedReader.readLine(BufferedReader.java:324) - locked <0x000000076b98ef30> (a java.io.InputStreamReader) at java.io.BufferedReader.readLine(BufferedReader.java:389) at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64) "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e40c000 nid=0x32dc waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e462800 nid=0x1b60 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001cd1e000 nid=0x2ba0 in Object.wait() [0x000000001f75e000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b586f58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) - locked <0x000000076b586f58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209) "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001cd1b800 nid=0x8cc in Object.wait() [0x000000001f65f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b586998> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157) - locked <0x000000076b586998> (a java.lang.ref.Reference$Lock) "main" #1 prio=5 os_prio=0 tid=0x0000000003034000 nid=0x15f4 waiting on condition [0x0000000002b2f000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at com.kgf.kgfjavalearning2021.jvm.JstackSleepTest.main(JstackSleepTest.java:11) "VM Thread" os_prio=2 tid=0x000000001e3d2800 nid=0x2eb0 runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000003049800 nid=0x396c runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000000000304b000 nid=0x3108 runnable "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x000000000304d000 nid=0x510 runnable "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x000000000304e800 nid=0xd24 runnable "GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000003050800 nid=0x31b4 runnable "GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000003052000 nid=0x39b8 runnable "GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000003056000 nid=0x1bd8 runnable "GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000003057000 nid=0x418 runnable "GC task thread#8 (ParallelGC)" os_prio=0 tid=0x0000000003058800 nid=0x22f4 runnable "GC task thread#9 (ParallelGC)" os_prio=0 tid=0x0000000003059800 nid=0x6ec runnable "VM Periodic Task Thread" os_prio=2 tid=0x000000001f9de800 nid=0x270 waiting on condition JNI global references: 33
从上面的输出文件中我们可以发现有一个限时等待线程,信息很详细,很容易定位到
四、创建一个线程间同步的程序代码,使用jstack命令查看
1、程序的代码
package com.kgf.kgfjavalearning2021.jvm; /*** * 测试线程的同步问题 */ public class JstackSyncTest public static void main(String[] args) Number number = new Number(); Thread t1 = new Thread(number); Thread t2 = new Thread(number); t1.setName("thread1"); t2.setName("thread2"); t1.start(); t2.start(); class Number implements Runnable private int number = 1; @Override public void run() while (true) synchronized (this) if (number<100) try Thread.sleep(500); catch (InterruptedException e) e.printStackTrace(); System.out.println(Thread.currentThread().getName()+":"+number); number++; else break;
上面的程序是一个线程同步程序,两个线程的状态可能在执行状态,也可能在限时睡眠,也可能处于阻塞状态
2、启动程序后使用jstack命令分析
命令:jstack pid
输出的文件内容如下:
2021-11-30 23:00:08 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.51-b03 mixed mode): "DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x0000000002ce4000 nid=0x3378 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "thread2" #13 prio=5 os_prio=0 tid=0x000000001f776800 nid=0xcd0 waiting for monitor entry [0x000000002071e000] java.lang.Thread.State: BLOCKED (on object monitor) at com.kgf.kgfjavalearning2021.jvm.Number.run(JstackSyncTest.java:31) - waiting to lock <0x000000076b92ff18> (a com.kgf.kgfjavalearning2021.jvm.Number) at java.lang.Thread.run(Thread.java:745) "thread1" #12 prio=5 os_prio=0 tid=0x000000001f756000 nid=0xe28 waiting on condition [0x000000002061f000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at com.kgf.kgfjavalearning2021.jvm.Number.run(JstackSyncTest.java:33) - locked <0x000000076b92ff18> (a com.kgf.kgfjavalearning2021.jvm.Number) at java.lang.Thread.run(Thread.java:745) "Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001f671000 nid=0x3520 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001e3ca800 nid=0x30d0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001e3ca000 nid=0x257c waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001e3c9000 nid=0x3910 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001e3c4800 nid=0x34d4 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001e2f9800 nid=0xffc runnable [0x000000001ff1e000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:170) at java.net.SocketInputStream.read(SocketInputStream.java:141) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) - locked <0x000000076b98ef40> (a java.io.InputStreamReader) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:161) at java.io.BufferedReader.readLine(BufferedReader.java:324) - locked <0x000000076b98ef40> (a java.io.InputStreamReader) at java.io.BufferedReader.readLine(BufferedReader.java:389) at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64) "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e111000 nid=0xd14 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e110800 nid=0x2b78 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001c9cd000 nid=0x29a8 in Object.wait() [0x000000001f41e000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b586f58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) - locked <0x000000076b586f58> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209) "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001c9cb800 nid=0x3888 in Object.wait() [0x000000001f31f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b586998> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157) - locked <0x000000076b586998> (a java.lang.ref.Reference$Lock) "VM Thread" os_prio=2 tid=0x000000001e082800 nid=0xc70 runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002cf9800 nid=0x2184 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002cfb000 nid=0x2ad0 runnable "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002cfd000 nid=0x36a0 runnable "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002cfe800 nid=0x1730 runnable "GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000002d00800 nid=0x1b7c runnable "GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000002d02000 nid=0x2968 runnable "GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000002d06000 nid=0x3bfc runnable "GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000002d07000 nid=0x3ba0 runnable "GC task thread#8 (ParallelGC)" os_prio=0 tid=0x0000000002d08800 nid=0x3054 runnable "GC task thread#9 (ParallelGC)" os_prio=0 tid=0x0000000002d09800 nid=0xddc runnable "VM Periodic Task Thread" os_prio=2 tid=0x000000001f676000 nid=0x1a04 waiting on condition JNI global references: 33
由上面的内容可以发现如下两个线程状态:
以上是关于JVM监控及诊断工具-命令之jstack的主要内容,如果未能解决你的问题,请参考以下文章