Operations 13 min read

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.

Programmer DD
Programmer DD
Programmer DD
Master JVM Performance: Essential Tools and Real‑World Tuning Guide

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 arguments

Example:

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-ip

Key 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 sizes

To 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.649

Column meanings (S0C, S1C, EC, etc.) correspond to survivor, eden, old, and permanent generation capacities and usage.

JVM heap layout
JVM heap layout

E. hprof (Heap/CPU profiling tool)

hprof can profile CPU usage and heap allocation.

java -agentlib:hprof[=options] ToBeProfiledClass
java -Xrunprof[:options] ToBeProfiledClass

Key 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 Hello

Example 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.

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 tuningMonitoring Toolsjstackjmapjps
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.