title: jvm工具_jcmd
date: 2017-02-15 09:18:05
tags:
category: JVM
jcmd,1.7发布新的jvm相关信息的诊断工具,Oracle官网建议的替代jmap的工具。注意其使用条件是只能在被诊断的JVM同台sever上,并且具有相同的用户和组(user and group).
jcmd最简单的用法是列出机器上启动的java进程。直接输出jcmd,在我的机器上显示如下:
25571 org.jetbrains.jps.cmdline.Launcher /Applications/IntelliJ IDEA.app/Contents/lib/javac2.jar:/Applications/IntelliJ IDEA.app/Contents/lib/snappy-in-java-0.5.1.jar:/Applications/IntelliJ IDEA.app/Contents/lib/jna.jar:/Applications/IntelliJ IDEA.app/Contents/lib/openapi.jar:/Applications/IntelliJ IDEA.app/Contents/lib/oromatcher.jar:/Applications/IntelliJ IDEA.app/Contents/lib/trove4j.jar:/Applications/IntelliJ IDEA.app/Contents/lib/jps-builders.jar:/Applications/IntelliJ IDEA.app/Contents/lib/nanoxml-2.2.3.jar:/Applications/IntelliJ IDEA.app/Contents/lib/jgoodies-forms.jar:/Applications/IntelliJ IDEA.app/Contents/lib/jdom.jar:/Applications/IntelliJ IDEA.app/Contents/lib/asm-all.jar:/Applications/IntelliJ IDEA.app/Contents/lib/protobuf-2.5.0.jar:/Applications/IntelliJ IDEA.app/Contents/lib/rt/jps-plugin-system.jar:/Applications/IntelliJ IDEA.app/Contents/lib/jna-platform.jar:/Applications/IntelliJ IDEA.app/Contents/lib/annotations.jar:/Applications/IntelliJ IDEA.app/Contents/lib/forms_rt.jar:/Applications/Intelli
21156 /Applications/SmartGit.app/Contents/Resources/Java/bootloader.jar
25542 org.jetbrains.idea.maven.server.RemoteMavenServer
25622 sun.tools.jcmd.JCmd
23880
25485
23647 org.apache.catalina.startup.Bootstrap start
24223
jdk还有个类似的工具“jps",也具有此功能。
jcmd指令后面通常接某一java进程的ID或者mainclass 加上各种指令,得到相关诊断信息。
而jcmd提供了相当丰富的指令,正常人都不可能记住……输入help指令,可以显示可选指令的列表。
tobideMacBook-Pro:hexoBlog tobi$ jcmd 23647 help
23647:
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
前面几个相关的功能,官网文档说明是可以线上动态诊断jvm信息的工具,但是要商用协议,没细研究,略过. 光看指令的名称,任然只能大概知道他的意思,如果想进一步了解该指令,继续在help 后面加上指令,可以看见详细说明,如下此指令的意思是打印jvm中线程的情况,impact表示对运行中的JVM的影响,permission,需要的权限,使用的语法:
jcmd 23647 help Thread.print
23647:
Thread.print
Print all threads with stacktraces.
Impact: Medium: Depends on the number of threads.
Permission: java.lang.management.ManagementPermission(monitor)
Syntax : Thread.print [options]
Options: (options must be specified using the or = syntax)
-l : [optional] print java.util.concurrent locks (BOOLEAN, false)
打印jvm中的线程细节
jcmd 23647 Thread.print -l
Thread.print -l
23647:
2017-02-15 10:27:29
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.112-b16 mixed mode):
"Attach Listener" #1598 daemon prio=9 os_prio=31 tid=0x00007f9ec22e4000 nid=0x441b waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"RMI TCP Connection(6)-127.0.0.1-EventThread" #1593 daemon prio=5 os_prio=31 tid=0x00007f9ebc8b0800 nid=0x7fbb waiting on condition [0x0000700011190000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x000000077ad4d708> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:494)
Locked ownable synchronizers:
- None
"RMI TCP Connection(10.18.19.28:12181)" #1592 daemon prio=5 os_prio=31 tid=0x00007f9eb887e000 nid=0x8f5f runnable [0x0000700010d84000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:117)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x000000077ad4bfa0> (a sun.nio.ch.Util$3)
- locked <0x000000077ad4bf90> (a java.util.Collections$UnmodifiableSet)
- locked <0x000000077ad4be70> (a sun.nio.ch.KQueueSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:349)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
Locked ownable synchronizers:
- None
"Curator-Framework-0-EventThread" #1591 daemon prio=5 os_prio=31 tid=0x00007f9ebf060000 nid=0xa563 waiting on condition [0x0000700010978000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x000000077f1aaa50> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:494)
Locked ownable synchronizers:
- None
如上,可以导出jvm中的线程细节。指令中的-l是打印线程所拥有的锁的情况。
另外jstack和jcmd Thread.print都具有发现死锁的功能。
2个线程分别持有锁A和锁B,去取对方的锁,java代码如下:
public class DeadLock {
public static void main(String[] args) throws InterruptedException {
ReentrantLock lockA = new ReentrantLock();
ReentrantLock lockB = new ReentrantLock();
Condition conditionA = lockA.newCondition();
Condition conditionB = lockB.newCondition();
new Thread(
() -> {
try {
lockA.lock();
TimeUnit.SECONDS.sleep(2L);
lockB.lock();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lockA.unlock();
lockB.unlock();
}
}
).start();
new Thread(
() -> {
try {
lockB.lock();
TimeUnit.SECONDS.sleep(2L);
lockA.lock();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lockA.unlock();
lockB.unlock();
}
}
).start();
TimeUnit.DAYS.sleep(1L);
}
}
运行起来后,使用jcmd pid Thread.print -l,会发现多了一些显示如下:
Found one Java-level deadlock:
=============================
"Thread-1":
waiting for ownable synchronizer 0x000000076abd6b20, (a java.util.concurrent.locks.ReentrantLock$NonfairSync),
which is held by "Thread-0"
"Thread-0":
waiting for ownable synchronizer 0x000000076abd6b50, (a java.util.concurrent.locks.ReentrantLock$NonfairSync),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x000000076abd6b20> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
at lazyguy.yyf.exercise.DeadLock.lambda$main$1(DeadLock.java:39)
at lazyguy.yyf.exercise.DeadLock$$Lambda$2/363771819.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"Thread-0":
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x000000076abd6b50> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
at lazyguy.yyf.exercise.DeadLock.lambda$main$0(DeadLock.java:23)
at lazyguy.yyf.exercise.DeadLock$$Lambda$1/668386784.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Found 1 deadlock.
jcmd自动为你发现了死锁~~
GC.class_stats和GC.class_histogram
显示jvm meta class的统计信息,impact high。
GC.heap_dump
导出堆的快照文件,可以配合jvisualm使用。
GC.run_finalization和GC.run
调用 java.lang.System.runFinalization().方法和java.lang.System.gc()方法。
VM.uptime
显示JVM启动的时间
VM.flags
打印虚拟机相关参数
VM.system_properties
打印当前jvm读到的system_properties,配合grep工具快速查看
VM.command_line
打印”启动“JVM的时候使用了那些指令
事实上jcmd是原来的
jps(java 进程查找)+jstack(线程信息)+jmap(堆和meta data内存信息)
这3样工具的集合加强版。