Working with JVM Diagnostics

1. Using JVM Flags (-XX:+PrintFlagsFinal)

FlagUse
-XX:+PrintFlagsFinalDump all flags + values
-XX:+PrintFlagsInitialDefaults only
-XX:+UnlockDiagnosticVMOptionsReveal diagnostic flags
-XX:+UnlockExperimentalVMOptionsReveal experimental flags

2. Using jcmd

CommandUse
jcmdList PIDs
jcmd PID helpList sub-commands
jcmd PID Thread.printThread dump
jcmd PID GC.heap_dump file.hprofHeap dump
jcmd PID JFR.start name=r duration=60s filename=r.jfrStart JFR
jcmd PID VM.system_propertiesProps snapshot

3. Generating Heap Dumps (-XX:+HeapDumpOnOutOfMemoryError)

ApproachUse
-XX:+HeapDumpOnOutOfMemoryErrorAuto-dump on OOM
-XX:HeapDumpPath=/tmpOutput dir
jcmd PID GC.heap_dump f.hprofOn-demand
AnalyzeEclipse MAT, VisualVM

4. Using Thread Dumps

MethodDetail
jstack PIDStack trace of all threads
jcmd PID Thread.printSame, modern
kill -3 PIDDump to stdout (Linux/macOS)
Look forBLOCKED, deadlocks, hot stacks

5. Analyzing GC Logs (-Xlog:gc)

FlagEffect
-Xlog:gcBasic GC log
-Xlog:gc*:file=gc.log:time,uptime,level,tagsFull
-Xlog:gc+heap=debugHeap detail
ToolsGCViewer, GCEasy.io

6. Using JFR (Java Flight Recorder)

CommandUse
JVM startup-XX:StartFlightRecording=duration=60s,filename=r.jfr
jcmd startjcmd PID JFR.start name=r duration=60s filename=r.jfr
jcmd stopjcmd PID JFR.stop name=r
AnalyzeJDK Mission Control (JMC)
Overhead~< 2% with default profile

7. Using JMX (Java Management Extensions)

APIUse
ManagementFactory.getMemoryMXBean()Heap stats
getThreadMXBean()Thread CPU
getGarbageCollectorMXBeans()GC counters
Remote-Dcom.sun.management.jmxremote.port=N

8. Profiling CPU Usage

ToolUse
async-profilerSampled CPU/alloc/lock; flame graphs
JFRMethod-sampling profiler
perf + perf-map-agentLinux native+JIT view
Mission ControlJFR analysis UI

9. Detecting Class Loading Issues

FlagEffect
-verbose:classLog every class load
-Xlog:class+load=debugModern unified logging
jcmd VM.class_hierarchyDump classes per loader
SymptomGrowing Metaspace = leak

10. Understanding JIT Compilation (-XX:+PrintCompilation)

FlagUse
-XX:+PrintCompilationMethods compiled
-XX:+UnlockDiagnosticVMOptions -XX:+PrintInliningInlining decisions
-XX:+PrintAssemblyNative code (needs hsdis)
Tier1=C1, 4=C2 (or Graal)

11. Using Mission Control for Analysis

JMC ViewReveals
Method ProfilingHot methods
MemoryAllocation pressure, OOM cause
GCPause distribution, generation activity
Lock InstancesContention hotspots
ExceptionsThrow rates
I/OFile / socket events