On this page
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
- tuts/java/jvm_and_openjdk/garbage_collection_g1_and_zgc Linked here; links to this page; shared tags: #java, #openjdk, #tier
- tuts/java/jvm_and_openjdk/jvm_classloading_bytecode_and_jit Linked here; links to this page; shared tags: #java, #openjdk, #tier
- tuts/java/jvm_and_openjdk/openjdk_distributions_and_toolchain Linked here; links to this page; shared tags: #java, #openjdk, #tier
- tuts/java/jvm_and_openjdk Linked here; links to this page; shared tags: #java, #openjdk
- tuts/java/tutorials Linked here; shared tags: #java
- tuts/interop/java_and_c_ffm_and_jni shared tags: #java, #openjdk, #tier
- tuts/java shared tags: #java, #openjdk
- tuts/java/builtins/AutoCloseable shared tags: #java, #tier
- tuts/java/builtins/enum shared tags: #java, #tier
- tuts/java/builtins/final shared tags: #java, #tier