jcmd 在安装 jdk 时已经安装,用于向本地jvm进程发送诊断命令。
$ jcmd -h
Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>
or: jcmd -l
or: jcmd -h
command must be a valid jcmd command for the selected jvm.
Use the command "help" to see which commands are available.
If the pid is 0, commands will be sent to all Java processes.
The main class argument will be used to match (either partially
or fully) the class used to start Java.
If no options are given, lists Java processes (same as -p).
PerfCounter.print display the counters exposed by this process
-f read and execute commands from the file
-l list JVM processes on the local machine
-h this help
$ jcmd
7200 sun.tools.jcmd.JCmd
10614 com.install4j.runtime.launcher.MacLauncher
88348 org.gradle.launcher.daemon.bootstrap.GradleDaemon 2.14
注意,7200 进程是jcmd本身。执行完jcmd后,该进程已经结束了。
在jcmd 后加上进程 ID,然后加上 help 。
$ jcmd 10614 help
10614:
The following commands are available:
JFR.configure
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.log
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.status
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
Compiler.directives_clear
Compiler.directives_remove
Compiler.directives_add
Compiler.directives_print
VM.print_touched_methods
Compiler.codecache
Compiler.codelist
Compiler.queue
VM.classloader_stats
Thread.print
JVMTI.data_dump
JVMTI.agent_load
VM.stringtable
VM.symboltable
VM.class_hierarchy
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.finalizer_info
GC.heap_info
GC.run_finalization
GC.run
VM.info
VM.uptime
VM.dynlibs
VM.set_flag
VM.flags
VM.system_properties
VM.command_line
VM.version
help
For more information about a specific command use 'help <command>'.
jcmd 后也可以跟上进程名:
$ jcmd MacLauncher help
输出结果和 jcmd 10614 help
相同。
$ jcmd 10614 VM.version
10614:
Java HotSpot(TM) 64-Bit Server VM version 9.0.1+11
JDK 9.0.1
$ jcmd 10614 VM.info
$ jcmd 10614 GC.run
$ jcmd 10614 GC.class_histogram | more
可以看到类名、对象数量、占用空间等。
$ jcmd 10614 VM.flags
$ jcmd 10614 VM.uptime
$ jcmd 10614 Thread.print
$ jcmd 10614 PerfCounter.print
$ jcmd 10614 GC.heap_dump $PWD/heap.dump
堆快照可以使用 VisualVM 等工具打开分析。
( 本文完 )