jstack:
在上一次【https://www.cnblogs.com/webor2006/p/10669472.html】jcmd中也可以获取线程的堆栈信息,回顾一下:
其实在JDK中还有另一个专门查看或导出Java应用程序中线程的堆栈信息jstack,具体瞅下它的使用:
咱们还是以之前死锁的MyTest3为例试一下该工具:
xiongweideMacBook-Pro:CoffeeMachineClient xiongwei$ jps -l
96336 org.gradle.launcher.daemon.bootstrap.GradleDaemon
94336
98744 sun.tools.jps.Jps
17418
98733 org.jetbrains.jps.cmdline.Launcher
96495 org.gradle.launcher.daemon.bootstrap.GradleDaemon
98734 com.jvm.memory.MyTest3
xiongweideMacBook-Pro:CoffeeMachineClient xiongwei$ jstack 98734
2019-04-12 16:32:16
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.92-b14 mixed mode): "Attach Listener" #12 daemon prio=9 os_prio=31 tid=0x00007fb1d7882000 nid=0x4003 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "DestroyJavaVM" #11 prio=5 os_prio=31 tid=0x00007fb1d81da800 nid=0x1703 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Thread-B" #10 prio=5 os_prio=31 tid=0x00007fb1d88a4000 nid=0x4703 waiting for monitor entry [0x0000700007a49000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.jvm.memory.A.method(MyTest3.java:17)
- waiting to lock <0x00000007958c2f10> (a java.lang.Class for com.jvm.memory.A)
at com.jvm.memory.B.method(MyTest3.java:35)
- locked <0x0000000795a0fcb0> (a java.lang.Class for com.jvm.memory.B)
at com.jvm.memory.MyTest3.lambda$main$1(MyTest3.java:10)
at com.jvm.memory.MyTest3$$Lambda$2/1989780873.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745) "Thread-A" #9 prio=5 os_prio=31 tid=0x00007fb1d7881000 nid=0x4803 waiting for monitor entry [0x0000700007946000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.jvm.memory.B.method(MyTest3.java:31)
- waiting to lock <0x0000000795a0fcb0> (a java.lang.Class for com.jvm.memory.B)
at com.jvm.memory.A.method(MyTest3.java:21)
- locked <0x00000007958c2f10> (a java.lang.Class for com.jvm.memory.A)
at com.jvm.memory.MyTest3.lambda$main$0(MyTest3.java:6)
at com.jvm.memory.MyTest3$$Lambda$1/2093631819.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745) "Service Thread" #8 daemon prio=9 os_prio=31 tid=0x00007fb1d8021000 nid=0x3b03 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C1 CompilerThread2" #7 daemon prio=9 os_prio=31 tid=0x00007fb1d9033000 nid=0x3903 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #6 daemon prio=9 os_prio=31 tid=0x00007fb1d7828800 nid=0x3803 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #5 daemon prio=9 os_prio=31 tid=0x00007fb1d9000000 nid=0x3703 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007fb1d7827000 nid=0x4b07 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007fb1d800d800 nid=0x3003 in Object.wait() [0x0000700007231000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795588ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x0000000795588ee0> (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=31 tid=0x00007fb1d800c800 nid=0x2f03 in Object.wait() [0x000070000712e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795586b50> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000795586b50> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) "VM Thread" os_prio=31 tid=0x00007fb1d7815800 nid=0x5403 runnable "GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007fb1d900d000 nid=0x2007 runnable "GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007fb1d7801000 nid=0x1e03 runnable "GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007fb1d7812000 nid=0x2a03 runnable "GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007fb1d7812800 nid=0x2c03 runnable "VM Periodic Task Thread" os_prio=31 tid=0x00007fb1d7829800 nid=0x3d03 waiting on condition JNI global references: 309 Found one Java-level deadlock:
=============================
"Thread-B":
waiting to lock monitor 0x00007fb1d90230b8 (object 0x00000007958c2f10, a java.lang.Class),
which is held by "Thread-A"
"Thread-A":
waiting to lock monitor 0x00007fb1d90259f8 (object 0x0000000795a0fcb0, a java.lang.Class),
which is held by "Thread-B" Java stack information for the threads listed above:
===================================================
"Thread-B":
at com.jvm.memory.A.method(MyTest3.java:17)
- waiting to lock <0x00000007958c2f10> (a java.lang.Class for com.jvm.memory.A)
at com.jvm.memory.B.method(MyTest3.java:35)
- locked <0x0000000795a0fcb0> (a java.lang.Class for com.jvm.memory.B)
at com.jvm.memory.MyTest3.lambda$main$1(MyTest3.java:10)
at com.jvm.memory.MyTest3$$Lambda$2/1989780873.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"Thread-A":
at com.jvm.memory.B.method(MyTest3.java:31)
- waiting to lock <0x0000000795a0fcb0> (a java.lang.Class for com.jvm.memory.B)
at com.jvm.memory.A.method(MyTest3.java:21)
- locked <0x00000007958c2f10> (a java.lang.Class for com.jvm.memory.A)
at com.jvm.memory.MyTest3.lambda$main$0(MyTest3.java:6)
at com.jvm.memory.MyTest3$$Lambda$1/2093631819.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745) Found 1 deadlock.
jmc:Java Mission Control(Java任务控制)
它也是一体化的可视化工具,相比jconsole或jvisualvm,它呈现的功能更多,而且还可以在程序运行期会不断实时的呈现变更的数据,下面瞅下它,还是以MyTest程序来实验:
其中里面提到了一个Java飞行记录器:
而在上一次【https://www.cnblogs.com/webor2006/p/10669472.html】中也提到了它:
JFR:Java Flight Recorder,它是可以实时的获取进程的统计数据。
咱们简单来使用一下它:
先来单击瞅下"MBean服务器":
是不是看到如此华丽的界面眼前一亮,而且还是实时跟踪的,确实是够强大,继续大致看一下:
其中上面出了一个元空间:
相当于将jcmd集成到这个工具里面了,接下来看一下飞行记录器:
我的天呀,比之前看到的界面还丰富好看,真的好强大,随便瞅一瞅其它的:
可以看到它里面还有很多功能切换:
另外对于JFR是可以用jcmd命令生成出飞行记录器的记录文件的,如下:
这里我们运行之前元空间的示例来用此工具来观察一下:
而且使用量会时时的进行变化:
也可以看到最大的元空间是200MB,正是我们在JVM上设置的大小参数:
然后当元空间溢出之后,则可以在jmc工具给出了一个断开提示:
关于该工具也只是做了一些基本的学习,具体在实际中的使用还得自己来实践,还是相当之强大的。
jhat:可以用来分析堆转储文件
这里得用之前的这个程序来演示了:
该程序会导致堆内存溢出,同时我们在JVM中设置了这样的参数:
运行,来看当堆内存溢出时生成的转储文件:
我们知道可以用jvisualvm可视化的工具来分析,那如果可视化的工具在实际场景中用不了怎么办呢?此时jhat就可以发挥作用了,咱们用它来分析一下该转储文件:
所以咱们可以用浏览器访问一下:
其中重点看一下我们的MyTest1,点击查看细节:
其中还可以看到也有OQL:
这里咱们简单的使用一下,先来感受下,具体详细的用法待之后有需要再现查,比如我们想查询当前是ClassLoader子类的名字,OQL可以这样写:
而我们在jvisualvm可视化的工具中也有OQL的查询,咱们在它里面来试一下:
至此,关于JDK的一些关键工具的使用就暂且学习到这了,在未来的JVM学习中这些工具是会派上用场的,这些工具的使用是比较简单的,但是如果不过一遍你都不知道有这些工具的存在,实际工作中其实就是怕有现成的东东可以解决某个实际问题,但是由于知识的局限性完全不知道有这么个东东,那。。是不是会很痛苦。