The jcmd Utility【jdk8+】

本文介绍如何使用jcmd工具发送诊断命令请求到JVM,以控制Java Flight Recordings,排查故障及诊断JVM和Java应用程序。文章还列举了多个实用命令,如打印HotSpot和JDK版本ID、打印系统属性、创建类直方图等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.6 The jcmd Utility

The jcmd utility is used to send diagnostic command requests to the JVM, where these requests are useful for controlling Java Flight Recordings, troubleshoot, and diagnose JVM and Java Applications. It must be used on the same machine where the JVM is running, and have the same effective user and group identifiers that were used to launch the JVM.

For more details on jcmd syntax and other usage details, see the jcmd command man page.

A special command jcmd <process id/main class> PerfCounter.print prints all performance counters in the process.

The command jcmd <process id/main class> <command> [options] sends the actual command to the JVM.

shows diagnostic command requests to JVM using jcmd utility.

Example 2-1 Diagnostic Command Requests with jcmd Utility

> jcmd
5485 sun.tools.jcmd.JCmd
2125 MyProgram
 
> jcmd MyProgram help (or "jcmd 2125 help")
2125:
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
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
 
> jcmd MyProgram help Thread.print
2125:
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 <key> or <key>=<value> syntax)
        -l : [optional] print java.util.concurrent locks (BOOLEAN, false)
 
> jcmd MyProgram Thread.print
2125:
2014-07-04 15:58:56
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.0-b69 mixed mode):
...

 

The following sections describe some useful commands and troubleshooting techniques with jcmd utility.

2.6.1 Useful Commands for jcmd Utility

The available diagnostic command may be different in different versions of HotSpot VM; therefore, using jcmd <process id/main class> help is the best way to see all available options. The following are some of the most useful commands that were in the tool since JDK 8. Remember you can always use jcmd <process id/main class> help <command> to get any additional options to these commands.

  • Print full HotSpot and JDK version ID

    jcmd <process id/main class> VM.version

  • Print all the system properties set for a VM

    There can be several hundred lines of information displayed.

    jcmd <process id/main class> VM.system_properties

  • Print all the flags used for a VM

    Even if you have provided no flags, some of the default values will be printed, for example initial and maximum heap size.

    jcmd <process id/main class> VM.flags

  • Print the uptime in seconds

    jcmd <process id/main class> VM.uptime

  • Create a class histogram

    The results can be rather verbose, so you can redirect the output to a file. Both internal and application specific classes are included in the list. Classes taking the most memory are listed at the top, and classes are listed in a descending order.

    jcmd <process id/main class> GC.class_histogram

  • Create a heap dump (hprof dump)

    jcmd GC.heap_dump filename=Myheapdump

    This is the same as using jmap -dump:file=<file> <pid>, but jcmd is the recommended tool to use.

  • Create a heap histogram

    jcmd <process id/main class> GC.class_histogram filename=Myheaphistogram

    This is the same as using jmap -histo <pid>, but jcmd is the recommended tool to use.

  • Print all threads with stack traces

    jcmd <process id/main class> Thread.print

2.6.2 Troubleshoot with jcmd Utility

The jcmd utility provides the following troubleshooting options:

  • Start a recording

    For example, to start a 2-minute recording on the running Java process with the identifier 7060 and save it to myrecording.jfr in the current directory, use the following:

    jcmd 7060 JFR.start name=MyRecording settings=profile delay=20s duration=2m filename=C:\TEMP\myrecording.jfr

  • Check a recording

    The JFR.check diagnostic command checks a running recording. For example:

    jcmd 7060 JFR.check

  • Stop a recording

    The JFR.stop diagnostic command stops a running recording and has the option to discard the recording data. For example:

    jcmd 7060 JFR.stop

  • Dump a recording

    The JFR.dump diagnostic command stops a running recording and has the option to dump recordings to a file. For example:

    jcmd 7060 JFR.dump name=MyRecording filename=C:\TEMP\myrecording.jfr

  • Create a heap dump

    The preferred way to create a heap dump is

    jcmd <pid> GC.heap_dump filename=Myheapdump

  • Create a heap histogram

    The preferred way to create a heap histogram is

    jcmd <pid> GC.class_histogram filename=Myheaphistogram

转载于:https://my.oschina.net/u/914290/blog/815749

01-Jul-2025 18:26:22.896 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [/ecm-sf5-cn] startup failed due to previous errors 01-Jul-2025 18:26:22.968 WARNING [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ecm-sf5-cn] appears to have started a thread named [Catalina-utility-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:4825) org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1172) org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1176) org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1176) org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1154) java.base@17.0.12/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) java.base@17.0.12/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) java.base@17.0.12/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) java.base@17.0.12/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) java.base@17.0.12/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63) java.base@17.0.12/java.lang.Thread.run(Thread.java:840) 01-Jul-2025 18:26:22.968 WARNING [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ecm-sf5-cn] appears to have started a thread named [DefaultQuartzScheduler_QuartzSchedulerThread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.base@17.0.12/java.lang.Object.wait(Native Method) org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:427) 01-Jul-2025 18:26:22.977 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ecm-sf5-cn] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@3777677a]) and a value of type [com.documentum.web.env.app.AppServerEnvironment] (value [[Name = App Server Environment, Description = App Server Environment Implementation, Major Version = 0, Minor Version = 1, Author = Documentum, Portal Description = App Server, Portal Version = x.x, Environment Base Version = 1.0, Environment Service Version = 6.0, Supports Dispatch Contract = true, Supports Theme Contract = true, Supports Error Contract = true, Supports FormOperationListener Contract = false, Supports Locale Contract = true, Supports Session Contract = true, Supports Tag Contract = false, Supports Render Contract = true, Supports Login Contract = false, Supports Security Contract = true, Supports ActionPrecondition Contract = false, Supports MessageService Contract = false, Supports SuperCredentials Contract = true, Supports Preference Contract = true, Supports ContentTransfer Contract = true]]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak. [2025-07-01 06:26:22,998] Artifact ecm-sf5-cn: Error during artifact deployment. See server log for details.
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值