Java:jcmd 命令的使用


#Java 工具#


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

查看正在运行的 Java 进程ID、名称和 main 函数参数

$ 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 相同。

查看某个进程的 JVM 版本

$ jcmd 10614 VM.version
10614:
Java HotSpot(TM) 64-Bit Server VM version 9.0.1+11
JDK 9.0.1

查看 JVM 进程信息

$ 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 等工具打开分析。


( 本文完 )