JVM监控及诊断工具-命令之jcmd

Posted 爱上口袋的天空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JVM监控及诊断工具-命令之jcmd相关的知识,希望对你有一定的参考价值。

1、简介

      在JDK1.7以后,新增了一个命令行工具jcmd它是一个多功能的工具,可以用来实现前面除jsat之外所有命令的功能。比如:用它来导出堆、内存使用、查看]ava进程、导出线程信
息、执行GC、JVM运行时间等。

     jcmd拥有jmap的大部分功能,并且在Oracle的官方网站上也推荐使
用jcmd命令代jmap命令

 


 2、命令:jcmd -l

可以查看当前正在运行的所有JVM进程,和jps有点类似

 


3、命令:jcmd pid PerfCounter.print

 这样我们就能看到12735这个进程的性能计数器


4、jcmd <pid> help

获得指定进程可用的命令列表

例如:

C:\\Users\\86136>jps
10192 Jps
3136 Launcher
14628 JstackSyncTest
7000
9592 RemoteMavenServer

C:\\Users\\86136>jcmd 14628 help
14628:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help

For more information about a specific command use 'help <command>'.

C:\\Users\\86136>

 由上可以发现所有可以执行的命令


5、jcmd pid GC.heap_dump -all 文件路径

  • 作用:生成Java堆Dump文件(HPROF格式)
  • 影响:高(取决于Java堆大小和内容。除非指定了-all选项,否则会导致Full GC
  • 所需权限:java.lang.management.ManagementPermission(monitor)
  • options:选项,必须使用key或者key=value的语法指定,可用的options如下:
    • -all:(可选)转储所有对象,包括不可达对象(BOOLEAN,false)
  • arguments:参数,可用的参数如下:
    • filename:Dump文件的名称(STRING,无默认值)

示例:

 windows生成的文件会自动保存到pid进程项目的目录下:

 


6、jcmd pid Thread.print -l 

  • 作用:打印所有带有堆栈跟踪的线程
  • 影响:中(取决于线程数)
  • 所需权限:java.lang.management.ManagementPermission(monitor)
  • options:选项,必须使用key或者key=value的语法指定,可用的options如下
    • -l:(可选)打印java.util.concurrent锁(BOLEAN,false)

示例:

可以看到下面的效果和jstack pid的效果一样 

C:\\Users\\86136>jcmd -l
3808 sun.tools.jcmd.JCmd -l
4104 org.jetbrains.jps.cmdline.Launcher D:/JetBrains/idea20190303/lib/netty-codec-4.1.41.Final.jar;D:/JetBrains/idea20190303/plugins/java/lib/maven-repository-metadata-3.3.9.jar;D:/JetBrains/idea20190303/plugins/java/lib/aether-impl-1.1.0.jar;D:/JetBrains/idea20190303/lib/jdom.jar;D:/JetBrains/idea20190303/plugins/java/lib/commons-lang3-3.4.jar;D:/JetBrains/idea20190303/plugins/java/lib/maven-model-3.3.9.jar;D:/JetBrains/idea20190303/plugins/java/lib/jps-builders-6.jar;D:/JetBrains/idea20190303/plugins/java/lib/plexus-component-annotations-1.6.jar;D:/JetBrains/idea20190303/plugins/java/lib/aether-transport-file-1.1.0.jar;D:/JetBrains/idea20190303/lib/httpcore-4.4.12.jar;D:/JetBrains/idea20190303/lib/commons-logging-1.2.jar;D:/JetBrains/idea20190303/plugins/java/lib/aether-dependency-resolver.jar;D:/JetBrains/idea20190303/plugins/java/lib/plexus-utils-3.0.22.jar;D:/JetBrains/idea20190303/lib/jna.jar;D:/JetBrains/idea20190303/lib/platform-api.jar;D:/JetBrains/idea20190303/plugins/java/lib/maven-aether-provider-3.3.9
7000
9592 org.jetbrains.idea.maven.server.RemoteMavenServer
14796 com.kgf.kgfjavalearning2021.jvm.JstackLockTest

C:\\Users\\86136>jcmd 14796 Thread.print -l
14796:
2021-12-01 21:26:30
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.51-b03 mixed mode):

"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x0000000002ed4000 nid=0x2044 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Thread-1" #13 prio=5 os_prio=0 tid=0x000000001f929800 nid=0xc2c waiting for monitor entry [0x00000000208ff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.kgf.kgfjavalearning2021.jvm.JstackLockTest$2.run(JstackLockTest.java:50)
        - waiting to lock <0x000000076b92cbf8> (a java.lang.StringBuilder)
        - locked <0x000000076b92cc40> (a java.lang.StringBuilder)
        at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
        - None

"Thread-0" #12 prio=5 os_prio=0 tid=0x000000001f8de800 nid=0x21d0 waiting for monitor entry [0x00000000207ff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.kgf.kgfjavalearning2021.jvm.JstackLockTest$1.run(JstackLockTest.java:26)
        - waiting to lock <0x000000076b92cc40> (a java.lang.StringBuilder)
        - locked <0x000000076b92cbf8> (a java.lang.StringBuilder)
        at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
        - None

"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001e5c9000 nid=0x3b70 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001e5c1000 nid=0x38b4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001e5c0000 nid=0xac0 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001e5bf800 nid=0x2930 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001e5be800 nid=0x3724 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001f822800 nid=0x39c4 runnable [0x00000000200ff000]
   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)

   Locked ownable synchronizers:
        - None

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e312000 nid=0x2d70 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e311800 nid=0x350c runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001cbbd000 nid=0x2fc0 in Object.wait() [0x000000001f5ff000]
   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)

   Locked ownable synchronizers:
        - None

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001cbbb800 nid=0x3abc in Object.wait() [0x000000001f4fe000]
   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)

   Locked ownable synchronizers:
        - None

"VM Thread" os_prio=2 tid=0x000000001e282800 nid=0x10c0 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002ee9800 nid=0xbd4 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002eeb000 nid=0xdd4 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002eed000 nid=0x35e0 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002eee800 nid=0x2de8 runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000002ef0800 nid=0x2888 runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000002ef2000 nid=0x36c8 runnable

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000002ef6000 nid=0x1350 runnable

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000002ef7000 nid=0xec4 runnable

"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x0000000002ef8800 nid=0x13d8 runnable

"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x0000000002ef9800 nid=0x31e8 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000000001e5c9800 nid=0x249c waiting on condition

JNI global references: 33


Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x0000000002fce708 (object 0x000000076b92cbf8, a java.lang.StringBuilder),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x0000000002fcd268 (object 0x000000076b92cc40, 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 <0x000000076b92cbf8> (a java.lang.StringBuilder)
        - locked <0x000000076b92cc40> (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 <0x000000076b92cc40> (a java.lang.StringBuilder)
        - locked <0x000000076b92cbf8> (a java.lang.StringBuilder)
        at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.


C:\\Users\\86136>

以上是关于JVM监控及诊断工具-命令之jcmd的主要内容,如果未能解决你的问题,请参考以下文章

JVM监控诊断之命令行使用

JVM监控诊断之命令行使用

JVM监控诊断之命令行使用

jvm 性能调优工具之 jcmd

JVM监控及诊断工具-命令之jhat

JVM监控及诊断工具-命令之jstat