jcmd is a command-line utility provided by the Java Development Kit (JDK) that allows you to interact with and manage Java processes. In this article, we'll explore various features of
jcmd tool provides various options to perform tasks such as:
List Java processes: You can use
jcmdto list all Java processes running on your system.
Print JVM information: You can get information about the system properties, VM flags, and command-line arguments of a Java process.
Perform garbage collection: You can request garbage collection for a Java process to free up memory and improve performance.
Thread stack trace: You can obtain the stack trace of all threads running in a Java process.
Heap dump: You can generate a heap dump of a Java process, which is useful for analyzing memory-related issues.
jcmd follows below syntax.
jcmd <PID> command
Keep in mind that some commands may require additional permissions or specific JDK versions. For more details and a list of available commands, you can run
jcmdwithout any arguments or check the official Oracle/OpenJDK documentation for
Printing running Java process
To print the Java process just type
jcmd in the terminal. This will display a list of Java processes along with their Process IDs (PIDs) and the main class or JAR file used to start the process.
jcmd 5783 jdk.jcmd/sun.tools.jcmd.JCmd
Print JVM information using
jcmd <PID> VM.system_properties
VM.system_properties command will display various system properties of the Java Virtual Machine, such as Java version, classpath, runtime environment details, and other relevant information.
Print VM flags
VM flags, also known as Java Virtual Machine (JVM) flags, are command-line options that you can specify when starting a Java application to configure various aspects of the JVM's behavior. These flags allow you to fine-tune the JVM's performance, memory management, and other settings. VM flags are specific to the Java Virtual Machine and may vary based on the version and implementation of the JVM you are using.
You can use the below syntax to print Vm flags.
java <VM flags> -jar <path-to-jar-file> jcmd <PID> VM.flags
Printing command line arguments
jinfo command will display various JVM information, including the command-line arguments used to start the Java process.
Perform garbage collection for any process
jcmd <PID> GC.run
GC.run command will instruct the Java Virtual Machine (JVM) to perform garbage collection on the specified Java process, freeing up memory and reclaiming unused objects.
Getting thread stack traces
jcmd <PID> Thread.print // redirect o/p to any file jcmd <PID> Thread.print > thread_dump.txt
Thread.print command will display the stack traces of all threads currently running in the specified Java process. This information can be valuable for diagnosing performance issues, deadlocks, or understanding the current state of the application.
Getting heap dump
A heap dump is a snapshot of the Java Virtual Machine's (JVM) heap memory at a specific point in time. It captures the state of all objects and data structures residing in the heap, including objects that are reachable and those that are no longer reachable (garbage). Heap dumps are useful for analyzing memory-related issues, identifying memory leaks, and understanding the memory usage patterns of a Java application.
Heap dumps provide valuable information to developers and system administrators, allowing them to:
Detect Memory Leaks: By analyzing the objects present in the heap, you can identify objects that are no longer needed but have not been garbage-collected. This helps you find memory leaks, which are often caused by unintentional object retention.
Analyze Memory Consumption: Heap dumps provide insights into the memory consumption of various data structures and objects, helping you optimize memory usage and improve application performance.
Troubleshoot OutOfMemoryErrors: When an "OutOfMemoryError" occurs, a heap dump can be generated to investigate the cause and understand which objects are consuming the most memory.
Inspect Object Contents: You can examine the contents of objects in the heap dump, which can be helpful for debugging and understanding the application's state at the time of the dump.
You can get heap dump for any process by using below syntax
jcmd <PID> GC.heap_dump <filename>
In this article, we have seen what is jcmd and how can we use to to perform various inspections of any running Java process. We have also seen examples of some of the most popular commands. To learn more about it, please visit https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html.
Did you find this article valuable?
Support Rahul kumar's team blog by becoming a sponsor. Any amount is appreciated!