Master JVM Performance: Essential Tools (jps, jstack, jmap, jstat, hprof)
Learn how to diagnose and resolve common Java performance issues such as OutOfMemoryError, thread deadlocks, and high CPU usage by using built‑in JVM tools—including jps, jstack, jmap, jhat, jstat, and hprof—through detailed command examples, output explanations, and practical profiling techniques.
Introduction
DK provides many convenient JVM performance tuning and monitoring tools. Besides integrated tools like VisualVM and jConsole, there are lightweight utilities such as jps, jstack, jmap, jhat, jstat, and hprof. This article introduces these tools to help developers understand and address common JVM performance problems.
Typical Enterprise Java Issues
OutOfMemoryError – insufficient memory
Memory leaks
Thread deadlocks
Lock contention
Excessive CPU usage by Java processes
These problems are often overlooked; developers may simply restart the server or increase memory without investigating the root cause. Understanding and solving them is essential for Java engineers.
A. jps (Java Virtual Machine Process Status Tool)
jps displays the status of JVM processes. Basic syntax: jps [options] [hostid] Options:
-q : suppress class name, JAR name, and main arguments
-m : show main arguments
-l : display the full package name of the main class or JAR
-v : display JVM arguments
Example output:
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
3149 org.apache.catalina.startup.Bootstrap start
...B. jstack
jstack shows thread stack traces of a Java process. Syntax: jstack [option] pid Key options:
-l : long listings, prints additional lock information (useful for deadlock analysis)
-m : mixed mode, includes native stack frames
Example workflow to find the most CPU‑intensive thread:
Identify the Java process ID (e.g., ps -ef | grep mrf-center).
Find the thread with highest CPU time using top -Hp <pid>.
Convert the thread ID to hexadecimal and grep it in the jstack output.
Sample commands and output:
root@ubuntu:/# ps -ef | grep mrf-center | grep -v grep
root 21711 1 1 14:47 pts/3 00:02:10 java -jar mrf-center.jar
root@ubuntu:/# printf "%x
" 21742
54ee
root@ubuntu:/# jstack 21711 | grep 54ee"PollIntervalRetrySchedulerThread" prio=10 tid=0x00007f950043e000 nid=0x54ee
in Object.wait() [0x00007f94c6eda000]The CPU consumption was in PollIntervalRetrySchedulerThread waiting on Object.wait(). The corresponding source code snippet is shown below:
C. jmap and jhat
jmap inspects heap memory usage, often used together with jhat for analysis. jmap [option] pid Common options:
-permstat pid : prints class loader and permanent generation statistics
-heap pid : shows heap configuration and usage
-histo[:live] pid : prints a histogram of object counts and sizes (live only if :live is added)
-dump:format=b,file=<file> pid : dumps the heap to a binary file
Example dump command:
root@ubuntu:/# jmap -dump:format=b,file=/tmp/dump.dat 21711
Dumping heap to /tmp/dump.dat ...
Heap dump file createdThe dump can be examined with tools like MAT, VisualVM, or jhat:
jhat -J-Xmx512m -port 9998 /tmp/dump.datD. jstat
jstat provides JVM statistics.
jstat [generalOption|outputOptions] vmid [interval[s|ms] [count]]Example showing GC statistics every 250 ms for four samples:
jstat -gc 21711 250 4Key columns explained (S0C, S1C, EC, OC, PC, YGC, YGT, FGC, FGCT, GCT, etc.) after understanding the JVM heap layout (young generation = Eden + two Survivor spaces; old generation; permanent generation).
E. hprof (Heap/CPU Profiling Tool)
hprof can profile CPU usage and heap memory.
java -agentlib:hprof=cpu=samples,interval=20,depth=3 Hello
java -agentlib:hprof=cpu=times Hello.java
java -agentlib:hprof=heap=sites Hello.java
java -agentlib:hprof=heap=dump Hello.javaSampling mode records CPU usage every 20 ms with a stack depth of 3, generating java.hprof.txt. Times mode provides finer‑grained CPU data via bytecode injection. Heap site mode records allocation sites, while heap dump mode produces a detailed heap dump. Using -Xrunprof:heap=sites in production is discouraged due to high overhead.
Signed-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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
