Essential Java Virtual Machine Command-Line Utilities
The Java Virtual Machine (JVM) provides several command-line tools for monitoring and debugging applications. These utilities offer insights into process status, runtime statistics, configuration, memory usage, and thread states.
jps: Process Status Tool
This tool lists JVM processes on the local machine. The -l option displays the full package name of the main class.
jps -l
Example output:
14224 org.jetbrains.jps.cmdline.Launcher
8528 org.jetbrains.idea.maven.server.RemoteMavenServer
236 com.example.MainApplication
jstat: Runtime Statistics Monitor
Use jstat to collect data on class loading, garbage collection, memory pools, and JIT compilation. The basic syntax is:
jstat [options] process_id [interval [count]]
To monitor garbage collection for process 236 every 250 milliseconds for 20 iterations:
jstat -gc 236 250 20
jinfo: Configuration Inspector
This utility prints JVM system properties and command-line flags for a specified process.
jinfo 236
Sample output includes Java version, system properties, and non-default VM flags like -XX:InitialHeapSize and -XX:+UseParallelGC.
jmap: Memory Map Analyzer
jmap generates heap dumps and displays memory usage details. Too show heap configuration and usage:
jmap -heap 236
The output breaks down heap regions (Eden, Survivor, Old Generation) with capacities and utilization percentages.
jstack: Thread Stack Trace Tool
For diagnosing deadlocks or high CPU usage, jstack captures thread dumps, showing each thread's stack trace.
jstack 236
Additional Tools
- jhat: Analyzes heap dump files (
.hprof). - hsdis: A library for disassembling JIT-compiled native code.
JConsole: Graphical Monitoring
Launch jconsole from the JDK's bin directory to connect to local or remote JVM processes. The interface provides real-time views of memory consumption, thread activity, class counts, and MBean attributes.