On this page
  1. Diagnostics, JFR & jcmd
  2. Overview
  3. jcmd โ€” The Swiss-Army Knife for Live JVMs
  4. JDK Flight Recorder (JFR)
  5. Starting & Inspecting a Recording
  6. Native Memory Tracking (NMT)
  7. Automatic Heap Dumps on OutOfMemoryError

Diagnostics, JFR & jcmd

Prev: Garbage Collection (G1 & ZGC) | Up: JVM & OpenJDK | Next: Tutorials
Rel: OpenJDK Distributions & Toolchain, JVM ClassLoading, Bytecode & JIT
Ref: OpenJDK Site , Java 21 API โ€” jdk.jfr
Tags: #public #java #openjdk #diagnostics #jfr #observability #tier/standard


Overview

One of OpenJDK's biggest operational strengths is its built-in, production-safe observability suite. You can inspect live threads, heap allocations, GC behavior, and CPU hotspots using jcmd and JDK Flight Recorder (JFR) without third-party agents or restarting the process.

jcmd โ€” The Swiss-Army Knife for Live JVMs

List running local JVM processes (and their PIDs):

jcmd -l

Common diagnostic commands against a running <pid>:

Command What It Inspects
jcmd <pid> help Lists all commands supported by that JVM
jcmd <pid> VM.flags Effective JVM flags (including ergonomics defaults)
jcmd <pid> VM.uptime Process uptime
jcmd <pid> Thread.print Full thread dump (stack traces & lock deadlocks)
jcmd <pid> Thread.dump_to_file -format=json threads.json Structured dump including virtual threads (Java 21+)
jcmd <pid> GC.heap_info Current heap and Metaspace usage
jcmd <pid> GC.class_histogram Top classes by instance count and retained shallow bytes
jcmd <pid> GC.heap_dump heap.hprof Full heap snapshot for offline leak analysis

JDK Flight Recorder (JFR)

JFR is an event recorder built directly into HotSpot with typically < 1% CPU overhead, making it safe to run continuously in production. It captures CPU execution samples, object allocation sites, lock contention, GC pauses, socket/file I/O, and custom application events.

Starting & Inspecting a Recording

# Record 60 seconds using the built-in "profile" settings
jcmd <pid> JFR.start name=cpu-check settings=profile duration=60s filename=recording.jfr

# Check active recordings
jcmd <pid> JFR.check

# Summarize the recording right in the terminal (no GUI required)
jfr summary recording.jfr
jfr print --events jdk.ExecutionSample recording.jfr | head -n 60

You can also start JFR at launch for always-on ring-buffer flight recording:

java -XX:StartFlightRecording=disk=true,maxsize=250m,maxage=1h,dumponexit=true,filename=exit.jfr -jar app.jar

Native Memory Tracking (NMT)

If RSS memory grows beyond -Xmx, enable Native Memory Tracking to inspect Metaspace, thread stacks, code cache, and direct ByteBuffer allocations:

java -XX:NativeMemoryTracking=summary -jar app.jar
jcmd <pid> VM.native_memory summary

Automatic Heap Dumps on OutOfMemoryError

In production services, add these flags so crashes leave actionable evidence:

java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/app/ -jar app.jar

Linked from

Related notes