Master JVM Performance: Essential Tools and Real‑World Tuning Guide
This article introduces the most common JVM performance problems in enterprise Java applications and provides a practical guide to using core monitoring and tuning tools such as jps, jstack, jmap, jstat and hprof, complete with command syntax, sample outputs and step‑by‑step troubleshooting examples.
In real‑world enterprise Java development you often encounter issues such as OutOfMemoryError, memory leaks, thread deadlocks, lock contention, and high CPU usage. Understanding and solving these problems is essential for Java developers.
A. jps (Java Virtual Machine Process Status Tool)
jps displays the status of JVM processes. Basic syntax: jps [options] [hostid] Common options:
-q Do not output class name, JAR name, or main arguments
-m Output main arguments
-l Output the full package name of the main class or JAR
-v Output JVM argumentsExample:
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
...B. jstack
jstack shows the thread stack of a Java process. Syntax:
jstack [option] pid
jstack [option] executable core
jstack [option] [server-id@]remote-hostname-or-ipKey options:
-l long listings (includes lock information, useful for deadlock analysis)
-m mixed mode (includes native stack frames)Typical workflow: find the Java process ID, locate the thread with the highest CPU usage, convert the thread ID to hexadecimal, and grep it from the jstack 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:# top -Hp 21711 # find thread ID 21742
root@ubuntu:# printf "%x
" 21742 # => 54ee
root@ubuntu:# jstack 21711 | grep 54ee
"PollIntervalRetrySchedulerThread" prio=10 tid=0x00007f950043e000 nid=0x54ee in Object.wait()The corresponding Java code shows an idle wait using sigLock.wait(timeUntilContinue).
C. jmap and jhat
jmap inspects heap memory usage; it is often used together with jhat for analysis.
jmap [option] pid
jmap -permstat pid # prints class loader and permanent generation info
jmap -heap pid # shows heap configuration and usage
jmap -histo[:live] pid # histogram of object counts and sizesTo dump a heap for offline analysis: jmap -dump:format=b,file=/tmp/dump.dat 21711 Analyze the dump with jhat: jhat -port 9998 /tmp/dump.dat If the dump is large, increase the maximum heap for jhat, e.g., jhat -J-Xmx512m -port 9998 /tmp/dump.dat.
D. jstat (JVM statistics monitoring tool)
jstat reports JVM performance metrics. Basic syntax:
jstat [ generalOption | outputOptions vmid [interval[s|ms] [count] ] ]Example showing GC statistics every 250 ms for four samples:
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.649Column meanings (S0C, S1C, EC, etc.) correspond to survivor, eden, old, and permanent generation capacities and usage.
E. hprof (Heap/CPU profiling tool)
hprof can profile CPU usage and heap allocation.
java -agentlib:hprof[=options] ToBeProfiledClass
java -Xrunprof[:options] ToBeProfiledClassKey options include heap=dump|sites|all, cpu=samples|times, output format, file name, sampling interval, etc.
Example of CPU sampling profiling:
java -agentlib:hprof=cpu=samples,interval=20,depth=3 HelloExample of heap allocation profiling: javac -J-agentlib:hprof=heap=sites Hello.java Note: enabling -Xrunprof:heap=sites on a production server incurs high overhead and is not recommended.
These tools together provide a comprehensive toolbox for diagnosing and tuning JVM performance in enterprise applications.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
