jcmd command-line utility tool to debug and diagnostic java application

In this blog post, learn JCMD command-line utility with examples.

jcmd command-line tool

JCMD is a command-line utility tool to diagnose the java process. It uses Java applications to debug the following use cases.

  • When a Java application is crashed
  • To know the Application Heap Memory and Garbage collection
  • troubleshoot and diagnose JVM applications

This tool is available as part of Java JDK installation.

It sends a diagnostic signal to retrieve JVM debugging information process id and class.

This single tool answers most of the below questions.

  • How to find a list of java running processes using jcmd without options
  • How to generate a thread dump for a java process using Thread. print
  • how to enable garbage collection to run from the command line using GC.run
  • How to manage remote management agent using ManagementAgent.stop/start/start_local
  • Native memory usage of the application using native_memory
  • list out system properties using VM.system_properties
  • Get a report of all properties of a JVM
  • display HotSpot and JDK version using VM.version
  • get all the flags printed using VM.flags
  • uptime of JVM application using VM.uptime
  • class memory usage histogram display using GC.class_histogram
  • how to retrieve Heap dump of java application using GC.heap_dump

JCMD command Syntax

jcmd

The options are -l, -of, and-h`. When there are no potions provided, It prints a list of java processes.

  • -f option allows reading the commands from the file
  • -l option displays a list of java process information
  • -h help option
B:\Workspace\modifysf>jcmd -l
4656 org.codehaus.plexus.classworlds.launcher.Launcher
5412 sun.tools.jcmd.JCmd -l
10444 com.kiran.MainApp

The above command gives the process id and process name running in JVM. Next, to get detailed diagnostic information with each process, we need to run one more command to get the list of commands.

B:\Workspace\modifysf>jcmd org.codehaus.plexus.classworlds.launcher.Launcher help
6604:
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

For more information about a specific command use 'help '.

How to take heap dump with jcmd tool?

The first list out all java processes using

$jcmd

This gives the list of all processes and ids.

Run the below command to take a heap dump of a process

jcmd (processid) GC.heap_dump (path)
  • process id is a number retrieved with the jcmd command
  • file-path location

java flight recorder stop/start/dump/check options

java flight recorder analyses the recordings and allows to get micro details about applications and JVM. This feature is available in java commercial license. To make use of these recordings, First, we need to enable them in the application

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder

  • JFR.stop - stop the recording
  • JFR.start - This option allows to start the recordings
  • JFR.dump - This dumps all the data JFR.
  • check - This returns the status of recording of a process

Native memory summary details

native memory usage will not be tracked. The application can be enabled by starting the application with -XX:NativeMemoryTracking=detail.

Here is the command for native memory for the specific process

jcmd org.codehaus.plexus.classworlds.launcher.Launcher VM.native_memory

JCMD command not found

Sometimes, you get the jcmd command not found When you are running the jcmd command in the terminal.

Here are the following steps to check.

  • First, make sure that JDK is installed or not checked by running the java -v command
  • if Java is not installed, please install the JDK from the official site.
  • And also please make sure that the environment variable for java is set. i.e JAVA_HOME

Conclusion

Learned jcmd command tutorial about taking heap and thread dump, Native memory summary, java flight recorder