Working with JVM Diagnostics
1. Using JVM Flags (-XX:+PrintFlagsFinal)
| Flag | Use |
|---|---|
-XX:+PrintFlagsFinal | Dump all flags + values |
-XX:+PrintFlagsInitial | Defaults only |
-XX:+UnlockDiagnosticVMOptions | Reveal diagnostic flags |
-XX:+UnlockExperimentalVMOptions | Reveal experimental flags |
2. Using jcmd
| Command | Use |
|---|---|
jcmd | List PIDs |
jcmd PID help | List sub-commands |
jcmd PID Thread.print | Thread dump |
jcmd PID GC.heap_dump file.hprof | Heap dump |
jcmd PID JFR.start name=r duration=60s filename=r.jfr | Start JFR |
jcmd PID VM.system_properties | Props snapshot |
3. Generating Heap Dumps (-XX:+HeapDumpOnOutOfMemoryError)
| Approach | Use |
|---|---|
-XX:+HeapDumpOnOutOfMemoryError | Auto-dump on OOM |
-XX:HeapDumpPath=/tmp | Output dir |
jcmd PID GC.heap_dump f.hprof | On-demand |
| Analyze | Eclipse MAT, VisualVM |
4. Using Thread Dumps
| Method | Detail |
|---|---|
| jstack PID | Stack trace of all threads |
| jcmd PID Thread.print | Same, modern |
| kill -3 PID | Dump to stdout (Linux/macOS) |
| Look for | BLOCKED, deadlocks, hot stacks |
5. Analyzing GC Logs (-Xlog:gc)
| Flag | Effect |
|---|---|
-Xlog:gc | Basic GC log |
-Xlog:gc*:file=gc.log:time,uptime,level,tags | Full |
-Xlog:gc+heap=debug | Heap detail |
| Tools | GCViewer, GCEasy.io |
6. Using JFR (Java Flight Recorder)
| Command | Use |
|---|---|
| JVM startup | -XX:StartFlightRecording=duration=60s,filename=r.jfr |
| jcmd start | jcmd PID JFR.start name=r duration=60s filename=r.jfr |
| jcmd stop | jcmd PID JFR.stop name=r |
| Analyze | JDK Mission Control (JMC) |
| Overhead | ~< 2% with default profile |
7. Using JMX (Java Management Extensions)
| API | Use |
|---|---|
ManagementFactory.getMemoryMXBean() | Heap stats |
getThreadMXBean() | Thread CPU |
getGarbageCollectorMXBeans() | GC counters |
| Remote | -Dcom.sun.management.jmxremote.port=N |
8. Profiling CPU Usage
| Tool | Use |
|---|---|
| async-profiler | Sampled CPU/alloc/lock; flame graphs |
| JFR | Method-sampling profiler |
| perf + perf-map-agent | Linux native+JIT view |
| Mission Control | JFR analysis UI |
9. Detecting Class Loading Issues
| Flag | Effect |
|---|---|
-verbose:class | Log every class load |
-Xlog:class+load=debug | Modern unified logging |
| jcmd VM.class_hierarchy | Dump classes per loader |
| Symptom | Growing Metaspace = leak |
10. Understanding JIT Compilation (-XX:+PrintCompilation)
| Flag | Use |
|---|---|
-XX:+PrintCompilation | Methods compiled |
-XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining | Inlining decisions |
-XX:+PrintAssembly | Native code (needs hsdis) |
| Tier | 1=C1, 4=C2 (or Graal) |
11. Using Mission Control for Analysis
| JMC View | Reveals |
|---|---|
| Method Profiling | Hot methods |
| Memory | Allocation pressure, OOM cause |
| GC | Pause distribution, generation activity |
| Lock Instances | Contention hotspots |
| Exceptions | Throw rates |
| I/O | File / socket events |