Master JVM Performance: Essential Tools and Real‑World Debugging Guide

This article introduces core JVM performance and monitoring utilities—including jps, jstack, jmap, jhat, jstat, and hprof—explains their command syntax, demonstrates practical steps to locate high‑CPU threads, analyze heap dumps, and interpret GC statistics, and provides concrete code examples for Java developers.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Master JVM Performance: Essential Tools and Real‑World Debugging Guide

1. jps – Basic JVM Process Status Tool

jps lists the Java processes running on a host. Syntax: jps [options] [hostid]. Common options are -q (no class/JAR name), -m (show main arguments), -l (full package name), and -v (JVM arguments). Example output shows process IDs, main classes, and arguments.

2. jstack – Thread Stack Dump

jstack prints stack traces of a Java process. Syntax: jstack [option] pid (or for core files and remote hosts). The -l option adds lock information, useful for dead‑lock analysis. A typical workflow to find the most CPU‑intensive thread is:

Identify the Java process ID with ps -ef | grep mrf-center.

Find the thread with highest CPU usage using top -Hp <pid> and note its numeric ID (e.g., 21742).

Convert the thread ID to hexadecimal (e.g., 0x54ee).

Run jstack <pid> | grep 54ee to locate the corresponding Java thread and stack trace.

The example reveals a thread named PollIntervalRetrySchedulerThread blocked in Object.wait(), and the related source code snippet is shown.

java -agentlib:hprof=cpu=samples,interval=20,depth=3 Hello

3. jmap and jhat – Heap Dump and Analysis

jmap exports heap information; jhat analyzes the dump. Syntax: jmap [option] pid. Useful options include: -heap – shows heap configuration and usage. -permstat – prints permanent generation statistics. -histo[:live] – prints a histogram of object counts and sizes. -dump:format=b,file=<path> – creates a binary heap dump.

Sample jmap -heap 21711 output displays GC algorithm, heap sizes, and per‑generation usage. The histogram command jmap -histo:live 21711 lists the most frequent object types. The binary dump can be opened with MAT, VisualVM, or jhat:

jhat -port 9998 /tmp/dump.dat

4. jstat – JVM Statistics Monitoring

jstat reports runtime statistics such as memory pool sizes and garbage‑collection activity. Syntax: jstat [generalOption|outputOptions] vmid [interval [count]]. Example showing GC metrics every 250 ms for four samples: jstat -gc 21711 250 4 The columns are explained (S0C/S1C – Survivor spaces, EC/EU – Eden, OC/OU – Old generation, PC/PU – Permanent generation, YGC/YGCT – young GC count/time, FGC/FGCT – full GC count/time, GCT – total GC time). An accompanying diagram illustrates the JVM heap layout.

5. hprof – Heap/CPU Profiling Tool

hprof can profile CPU usage and heap allocation. Invocation forms include:

java -agentlib:hprof[=options] Class
java -Xrunprof[:options] Class
javac -J-agentlib:hprof[=options] Class

Key options (default values in brackets) are: heap=dump|sites|all – heap profiling mode. cpu=samples|times|old – CPU profiling mode. monitor=y|n – monitor lock contention. format=a|b – text or binary output. file=<file> – output file name (default java.hprof[.txt]). interval=<ms> – sampling interval (default 10 ms).

… (other options omitted for brevity).

Example commands:

java -agentlib:hprof=cpu=samples,interval=20,depth=3 Hello
javac -J-agentlib:hprof=cpu=times Hello.java
javac -J-agentlib:hprof=heap=sites Hello.java
javac -J-agentlib:hprof=heap=dump Hello.java

Note: using -Xrunprof on production servers incurs significant overhead and is not recommended.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMperformance tuningjstackjmapjstat
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.