Master JVM Performance: Essential jps, jstack, jmap, jstat & hprof Tools
This article introduces essential JVM performance monitoring and profiling tools—including jps, jstack, jmap, jhat, jstat, and hprof—explaining their syntax, options, and practical usage for diagnosing memory leaks, thread deadlocks, high CPU consumption, and other common Java production issues.
Background
In enterprise Java development and maintenance, developers often encounter problems such as OutOfMemoryError, memory leaks, thread deadlocks, lock contention, and high CPU usage. Understanding and solving these issues is essential for advancing as a Java programmer.
jps
jps outputs information about JVM processes. Syntax: jps [options] [hostid] Common options:
-q Do not output class name, JAR name, or main arguments
-m Output arguments passed to the main method
-l Output the full package name of the main class or JAR
-v Output arguments passed to the JVMExample:
root@ubuntu:/# jps -m -l
2458 org.artifactory.standalone.main.Main /usr/local/artifactory-2.2.5/etc/jetty.xml
29920 com.sun.tools.hat.Main -port 9998 /tmp/dump.dat
...jstack
jstack displays thread stack traces for a Java process. Syntax:
jstack [option] pid
jstack [option] executable core
jstack [option] [server-id@]remote-hostname-or-ipKey options:
-l Long listings, prints additional lock information (useful for deadlocks)
-m Mixed mode, includes native stack framesExample to find the most CPU‑intensive thread:
# ps -ef | grep mrf-center | grep -v grep
root 21711 1 1 14:47 pts/3 00:02:10 java -jar mrf-center.jar
# top -Hp 21711 (identify thread ID 21742)
# printf "%x
" 21742
54ee
# jstack 21711 | grep 54ee
"PollIntervalRetrySchedulerThread" prio=10 tid=0x00007f950043e000 nid=0x54ee in Object.wait() [0x00007f94c6eda000]The identified thread is waiting in PollIntervalRetrySchedulerThread ’s Object.wait() call.
jmap and jhat
jmap exports heap memory information; jhat analyzes the exported heap dump.
jmap [option] pid
jmap -heap pid
jmap -histo[:live] pid
jmap -dump:format=b,file=dumpFileName pidExample of heap summary:
jmap -heap 21711
Attaching to process ID 21711, please wait...
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 2067791872 (1972.0MB)
...To analyze a dump:
# jmap -dump:format=b,file=/tmp/dump.dat 21711
Dumping heap to /tmp/dump.dat ...
Heap dump file created
# jhat -port 9998 /tmp/dump.dat
Started HTTP server on port 9998jstat
jstat monitors JVM statistics such as memory usage and garbage collection.
jstat [ generalOption | outputOptions vmid [interval[s|ms] [count]] ]Example showing GC information:
# jstat -gc 21711 250 4
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
192.0 192.0 64.0 0.0 6144.0 1854.9 32000.0 4111.6 55296.0 25472.7 702 0.431 3 0.218 0.649
...Column meanings:
S0C, S1C – Survivor space capacities
S0U, S1U – Survivor space usage
EC, EU – Eden space capacity and usage
OC, OU – Old generation capacity and usage
PC, PU – Permanent generation capacity and usage
YGC, YGCT – Young GC count and time
FGC, FGCT – Full GC count and time
GCT – Total GC timehprof
hprof provides CPU usage and heap profiling.
java -agentlib:hprof[=options] ToBeProfiledClass
java -Xrunprof[:options] ToBeProfiledClass
javac -J-agentlib:hprof[=options] ToBeProfiledClassKey options include:
heap=dump|sites|all Heap profiling
cpu=samples|times CPU usage profiling
monitor=y|n Monitor contention
format=a|b Output format (text or binary)
file=<file> Output file name
interval=<ms> Sampling interval in ms
depth=<size> Stack trace depthExample of CPU sampling profiling:
java -agentlib:hprof=cpu=samples,interval=20,depth=3 HelloExample of CPU times profiling: javac -J-agentlib:hprof=cpu=times Hello.java Example of heap allocation profiling: javac -J-agentlib:hprof=heap=sites Hello.java Example of heap dump generation:
javac -J-agentlib:hprof=heap=dump Hello.javaSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
