Sub topic 4
First look with jcmd, jstat and jstack
This chapter assumes you can start a Java process from the command line, read a stack trace, and know what the heap and garbage collector (GC) are. The JDK ships with small command-line tools that show what a running JVM is doing. You can use them before you set up a full profiler.
Find the process
Every tool below needs the process ID (pid) of the target JVM. Run jcmd with no arguments to list the Java processes you can see.
jcmdEach line shows a pid and the main class or jar. The tools attach to the JVM as the same operating system user that started it. Running as a different user usually fails with a connection error.
jcmd: the general-purpose tool
jcmd <pid> help lists the commands the target JVM supports. These are the ones you will use most often:
| Command | What it gives you |
|---|---|
| `VM.flags` | The JVM flags in effect, including defaults picked by the JVM |
| `VM.uptime` | How long the JVM has been running |
| `Thread.print` | A thread dump |
| `GC.heap_info` | Heap size and usage per region |
| `GC.class_histogram` | Instance counts and bytes per class |
| `JFR.start` | Starts a Flight Recorder recording |
jcmd <pid> VM.flags
jcmd <pid> GC.heap_info
jcmd <pid> Thread.print > threads.txtjstack <pid> prints the same thread dump as Thread.print. Prefer jcmd because one tool covers every task.
jstat: GC over time
jstat samples the JVM's counters at an interval. The -gcutil option shows how full each heap region is as a percentage, plus GC counts and times.
jstat -gcutil <pid> 1000The last argument is the interval in milliseconds, so this prints one line per second. Watch these columns:
Ois old generation occupancy. If it climbs steadily and never drops after a full collection, suspect a leak.YGCandYGCTare the count and total time of young collections. A count that rises quickly means a high allocation rate.FGCandFGCTare the same for full collections. Any steady increase deserves attention.GCTis the total time spent in GC. Compare it with uptime to get the fraction of time the JVM spends collecting.
Column names differ slightly between collectors, so check the header line.
Reading a thread dump
A thread dump lists every thread with its state and stack. Take two or three dumps a few seconds apart, because one dump is a single moment and can mislead.
RUNNABLEthreads are running or ready to run. Many threads with the same top frame point to a hot spot.BLOCKEDthreads are waiting for a monitor lock. The linewaiting to lock <0x...>names the lock, and alocked <0x...>line in another thread names the owner.WAITINGandTIMED_WAITINGthreads are parked, sleeping, or waiting on a condition. They are often idle pool workers and are usually not a problem.
A first-look routine
These tools give you clues, not measurements. Use them to decide which deeper tool to reach for next.
Exercise
In jstat -gcutil output, O stays near 95% and FGC increases every few seconds. What does that suggest, and which command would you run next?
Show answer
The old generation is nearly full and full collections cannot free much, which suggests a memory leak or a heap that is too small. Run jcmd <pid> GC.class_histogram to see which classes hold the most memory, then take a heap dump with jcmd <pid> GC.heap_dump for a closer look.