32.1 Diagnostic Tools

Use built-in JDK tools to capture diagnostic data and troubleshoot production issues.

jcmd (Unified Diagnostic Command)

# List running Java processes
jcmd

# Print VM flags
jcmd <pid> VM.flags

# Trigger heap dump
jcmd <pid> GC.heap_dump /tmp/heap.hprof

# Print thread dump
jcmd <pid> Thread.print

# Start JFR recording
jcmd <pid> JFR.start name=myrecording settings=profile filename=/tmp/rec.jfr

Heap Dumps (jmap)

jmap -dump:live,format=b,file=/tmp/heap.hprof <pid>

Analyze with Eclipse Memory Analyzer (MAT) or VisualVM.

Thread Dumps (jstack)

jstack <pid> > threads.txt

Identify deadlocks, blocked threads, and CPU-spinning threads.

GC Logs

java -Xlog:gc*:file=gc.log:time,uptime,level,tags MyApp

Analyze with GCEasy or GCViewer.

Analyzing Heap Dumps

  1. Identify large objects: Look for retained size in MAT.
  2. Find leak suspects: MAT's leak suspects report.
  3. Histogram: Class instances and shallow heap usage.

Thread Dump Analysis

"http-nio-8080-exec-10" #42 daemon prio=5 waiting on condition
  java.lang.Thread.State: WAITING (parking)
    at jdk.internal.misc.Unsafe.park()
    at java.util.concurrent.locks.LockSupport.park()
  • WAITING/TIMED_WAITING: Idle or waiting for I/O.
  • BLOCKED: Contention on synchronized block.
  • RUNNABLE: Active execution.

Triage Workflow

  1. High CPU: Thread dump + JFR profiling → identify hot methods.
  2. High Memory: Heap dump → find large objects or leaks.
  3. Slow Response: Thread dump → check blocked/waiting threads.
  4. Crashes: hs_err_pid*.log → JVM crash log analysis.

Guidance

  • Use jcmd as the primary diagnostic tool (replaces jmap, jstack, jinfo).
  • Capture multiple thread dumps (3–5, 10s apart) to identify patterns.
  • Analyze heap dumps offline to avoid impacting production.
  • Enable GC logs by default in production for post-mortem analysis.
  • Combine diagnostic data: heap + thread dumps + JFR + GC logs.