jcmd 工具在 Java 9 中的重要性是什么?
javaobject oriented programmingprogramming更新于 2025/11/6 18:20:17
"jcmd"是 JVM 诊断 工具,它是一个命令行工具,用于在本地机器上针对给定的 JVM 运行诊断命令。此工具自 Java 7 版本起已包含在 JDK 安装中,可以用"%java_home%\bin\jcmd.exe"程序文件表示。如果我们在环境变量的"path"中包含"%java_home%\bin"目录,我们可以运行"jcmd -h"命令来查看所有选项的完整列表,如下所示
C:\Users\User>jcmd -h Usage: jcmd 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 -l). 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
示例
public class JCmdToolTest {
public static void main(String args[]) {
Runtime runtime = Runtime.getRuntime();
System.out.println("Free memory: " + runtime.freeMemory());
System.out.println("Total memory: " + runtime.totalMemory());
try {
Thread.sleep(5000);
} catch(InterruptedException e) {
}
}
}
输出
Free memory: 65454560 Total memory: 67108864
我们可以使用"jcmd -l"命令列出本地机器上所有正在运行的 JVM,然后使用输出中的 PID 或类 main 来识别 JVM。
C:\Users\User>jcmd -l 6108 jdk.jcmd/sun.tools.jcmd.JCmd -l
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

