Master Java’s Built‑In Diagnostic Tools: jps, jstat, jinfo, jmap, jhat & jstack
This guide explains where Java's command‑line tools reside, why they are small wrappers around tools.jar, and provides detailed usage, options, and examples for the most common diagnostics utilities—including jps, jstat, jinfo, jmap, jhat, and jstack—helping developers monitor and troubleshoot JVM processes effectively.
Environment: JDK 1.8.0_92. The command‑line tools are located in %JAVA_HOME%\bin.
Most tools are about 17 KB because the executable files are thin wrappers around classes in %JAVA_HOME%\tools.jar.
Below is an overview of commonly used tools.
jps Displays all currently running HotSpot JVM processes. Options:
-l : prints the full package and class name, or the jar file path if the process runs a jar.
-m : prints the arguments passed to the JVM at startup.
-v : prints the JVM arguments used to start the process.
-q : prints only the process ID.
Example program:
<code>public class JpsMain {
public static void main(String[] args) throws Exception {
System.out.println(Arrays.toString(args));
System.in.read();
}
}
</code>Running the program with three arguments (a b c) and then executing jps -l -m -v -q shows the corresponding output.
jstat Monitors various JVM runtime statistics such as class loading, memory usage, and garbage‑collection activity. Usage:
<code>jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]</code>where vmid is the JVM process ID, interval is the query interval (s or ms), and count is the number of queries. Common options:
-class : class loading/unloading statistics.
-compiler : JIT compilation statistics.
-gc : heap usage and GC timing.
-gcutil : same as
-gcbut shows used‑space percentages.
-gcnew : young‑generation GC information.
-gcold : old‑generation GC information.
Example: jstat -gc 16480 3s reports GC data every three seconds.
jinfo Displays and modifies JVM flags of a running process. Usage:
<code>jinfo <option> <pid></code>Example: jinfo -flag CICompilerCount 16480 shows the value of the CICompilerCount flag.
jmap Generates heap‑dump snapshots and provides heap‑related information. Usage:
<code>jmap [option] <pid></code>Key options:
-dump : creates a heap dump (e.g.,
jmap -dump:live,format=b,file=heap.bin <pid>).
-heap : prints detailed heap configuration.
-histo : shows a histogram of objects in the heap.
jhat Analyzes a heap‑dump file. Usage: jhat <file> After generating a dump, run jhat heap.bin and open the provided HTTP service (default port 7000) in a browser.
jstack Produces a thread dump of a running JVM. Usage:
<code>jstack [option] <pid></code>Options:
-F : forces a thread dump when the JVM does not respond.
-l : includes lock information.
-m : shows native stack frames for C/C++ methods.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.