⚙️ JVM Internals & Memory · Advanced

Diagnostic tools in Java

jcmd, jstack, jmap, JDK Flight Recorder, Mission Control, heap dumps.

🧩 The mystery3 a.m.: requests are hanging, CPU is at 0%, the logs say nothing. The JDK ships a tool that can show you, line by line, what every thread is doing right now.

jcmd, the all-rounder

Run **jcmd with no arguments to list running JVMs** and their PIDs. Then pass a PID plus a command: thread dumps, heap dumps, flags, Flight Recorder. Older single-purpose tools: jstack, jmap, jstat.

jcmd                       # list JVMs
jcmd 4242 Thread.print     # thread dump
jcmd 4242 GC.heap_dump h.hprof
jcmd 4242 VM.flags

Photo vs inventory

A thread dump is a photo: what every thread is executing right now and which locks it holds or waits for. Perfect for deadlocks and stuck requests. A heap dump is an inventory of every object in memory. jstat prints GC statistics live.

🔮 Predict it

Hanging, but idle

Requests hang and CPU is near 0%. What should you capture first?

  1. Several thread dumps a few seconds apart
  2. A heap dump
  3. The output of javap
Show the answer

Low CPU plus hanging requests means threads are waiting: on locks, I/O or a pool. Take several thread dumps a few seconds apart: threads stuck at the same place in every dump reveal the real blocker.

Flight Recorder

JDK Flight Recorder (JFR) is built into the JVM and records events (allocations, locks, GC, I/O) with low enough overhead to run continuously in production. Dump a recording when something goes wrong and analyze it in JDK Mission Control.

jcmd 4242 JFR.start duration=60s \
     filename=rec.jfr
⚠️ The trap

Sorting by shallow size

In a heap dump analyzer, sorting by shallow size (the object itself) shows boring byte[] and String at the top. Sort by retained size: how much memory would be freed if that object were collected. A tiny HashMap can retain 3 GB.

💼 In the real world

An incident runbook

Hangs: several thread dumps. Memory growth: a heap dump, then retained sizes and paths to GC roots. Vague slowness: a JFR recording. Writing these commands into your team's runbook before the incident saves precious minutes during one.

Key takeaways

  1. jcmd <pid> Thread.print = thread dump
  2. jcmd <pid> GC.heap_dump file.hprof = heap dump
  3. JFR: low-overhead recording, analyzed in Mission Control
  4. Retained size shows what an object keeps alive

💡 A thread dump is a photo of what everyone is doing right now; a heap dump is an inventory of the whole warehouse.

🤯 Did you know?

When a thread dump finds threads deadlocked on Java monitors, it says so explicitly with a "Found one Java-level deadlock" section at the end.

Practice questions

Requests hang and CPU is near 0%. What should you capture first?

  1. A GC log from last week
  2. The output of javap
  3. A heap dump
  4. Several thread dumps a few seconds apart
Check your answer

Several thread dumps a few seconds apart. Low CPU plus hanging requests suggests threads are waiting on locks, I/O or pools. Thread dumps show exactly where they are blocked.

Which JDK command, run with no arguments, lists the running Java processes and their PIDs?

  1. jlink
  2. jcmd
  3. jdeps
  4. javap
Check your answer

jcmd. jcmd with no arguments lists local JVMs. You then pass a PID plus a command, like `jcmd 4242 Thread.print`.

Heap dumps count the bytes of every object. Next: what exactly is inside those bytes. Object headers and alignment.