How would you use jstat to monitor GC activity and heap generation sizes for a running JVM process in real time, without needing a full profiling tool?
Short answer
jstat -gcutil prints periodic snapshots of each heap generation's utilization percentage (young generation survivor spaces, Eden, old generation, and metaspace) along with cumulative GC event counts and total time spent in young/full GC, providing a lightweight, always-available…
Advertisement
AModel answer
jstat -gcutil <pid> <interval> prints periodic snapshots of each heap generation's utilization percentage (young generation survivor spaces, Eden, old generation, and metaspace) along with cumulative GC event counts and total time spent in young/full GC, providing a lightweight, always-available (bundled with the JDK, no extra setup) way to observe GC behavior and heap pressure trends in real time directly from the command line, useful for quick production diagnostics without needing to attach a heavier profiling tool.
Example
# Print GC utilization stats every 1 second, indefinitely jstat -gcutil <pid> 1000 # Sample output columns include: # S0/S1 (survivor space %), E (Eden %), O (old gen %), M (metaspace %), YGC/YGCT (young GC count/time), FGC/FGCT (full GC count/time)
Real-world example
An on-call engineer investigating a suspected memory pressure issue in a running production JVM runs jstat -gcutil directly against the process ID, immediately observing the old generation utilization climbing steadily toward 100% between full GCs, a lightweight first diagnostic step before reaching for a heavier tool like a full heap dump analysis.
Likely follow-ups
- How would you interpret a pattern of frequent full GCs with the old generation utilization dropping only slightly after each one?
- What's the difference between jstat's snapshot-based monitoring and continuous GC logging via -Xlog:gc?
Related topics