skip to content

jcmd

jcmd is the one tool that has absorbed the rest: heap dumps, class histograms, thread prints, VM flags and JFR control, all through a single command. Mentioning that it is the modern replacement for jmap and jstack is what interviewers listen for.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is jcmd, and why is it considered the modern, recommended replacement for one-off tools like jmap and jstack?

level: juniorimportance: must knowfreq 70%

answer

  1. One tool, many sub-commands (Thread.print, GC.*, VM.*, JFR.*)
  2. jcmd with no args = list JVMs; jcmd <pid> help = list commands
  3. Uses the cooperative attach mechanism, same user/host
  4. Replaces jmap/jstack/jinfo (legacy, less safe)
  5. Identify target by PID or main-class name

basics

~20 s

jcmd is a JDK command-line tool that sends diagnostic commands to a running JVM. Instead of separate tools, one tool can dump the heap, print threads, run GC, show flags, and more. It's safer and unified, so it replaces jmap and jstack.

solid answer

~40 s

jcmd is a unified diagnostic command tool shipped with the JDK (in the bin directory). You point it at a running JVM by process id and issue a sub-command such as Thread.print, GC.heap_dump, GC.class_histogram, VM.flags, or JFR.start. It talks to the target over the JVM's attach mechanism, so the target must be the same user (and usually the same machine). It is the recommended replacement for the older one-purpose tools because those (jmap, jstack, jinfo) are deprecated/less maintained, each used a slightly different attach path, and some forced unsafe modes. jcmd consolidates everything behind one interface, exposes the live list of supported commands via 'jcmd <pid> help', and uses only the safe serviceability path. Running 'jcmd' with no arguments lists all attachable JVMs.

go deeper

for a junior

Knows jcmd is a CLI tool to inspect a running JVM and that 'jcmd' lists JVMs while 'jcmd <pid> help' lists commands.

for a middle

Can name the main command families (Thread.print, GC.heap_dump, VM.flags, JFR.*) and explain it replaces jmap/jstack.

for a senior

Explains the attach mechanism, same-user/same-host constraint, and why jcmd is safer and the maintained path versus the legacy one-off tools.

for a principal

Frames jcmd within a production diagnostics strategy: when to use it vs always-on JFR/agents, attach-mechanism security implications, and standardizing runbooks around it.

## What jcmd is **The JVM (Java Virtual Machine)** is the process that runs your Java program. While it runs, it holds rich internal state: the heap (where objects live), all the threads, the chosen garbage-collector settings, system properties, and more. To *inspect or influence* that state from outside, the JDK ships a family of command-line **diagnostic tools** in its `bin/` directory. Historically these were *one tool per job*: `jmap` (heap dumps, histograms), `jstack` (thread dumps), `jinfo` (flags/properties), `jstat` (GC statistics). **jcmd** is a single tool that subsumes most of them. You give it a target JVM and a *sub-command*, and it asks that JVM to perform a diagnostic action and return the result. ## How you invoke it ``` jcmd # list every JVM you can attach to (pid + main class) jcmd <pid> help # list the diagnostic commands THIS jvm supports jcmd <pid> help <command> # show the arguments for one command jcmd <pid> <command> [args] # run it ``` The target is identified by its **PID (process id)**. You can also use the main-class name instead of a numeric pid. Examples: ``` jcmd 4242 Thread.print jcmd 4242 GC.heap_dump /tmp/app.hprof jcmd MyApp VM.flags ``` ## Why it works — the attach mechanism jcmd does not read another process's memory directly. It uses the JVM **attach API / serviceability agent path**: it signals the target JVM, the target spins up an internal attach listener, and the two communicate over a local socket (a file under the temp dir on Linux/macOS). Because of this: - The target JVM must be **running** and must accept attach (it does by default unless `-XX:+DisableAttachMechanism` was set). - jcmd must run as the **same OS user** as the target (or with sufficient privilege) and normally **on the same host** — it is not a remote tool. ## Why it replaces jmap / jstack 1. **Unification.** One binary, one mental model, one help system. Discoverability is built in: `jcmd <pid> help` lists exactly what the *running* JVM supports. 2. **Safety.** The old tools could fall back to a *forced* mode that paused or even risked the target. jcmd uses only the cooperative attach path, so it is safer on production JVMs. 3. **Maintenance.** `jmap`/`jstack`/`jinfo` are effectively legacy (some marked for removal); new diagnostic capabilities (JFR control, finer GC commands, NMT) are added to jcmd, not to the old tools. 4. **No flag-juggling.** Each legacy tool had its own flags and quirks; jcmd presents a consistent `command arg=value` grammar. ## The command families you should know - **Threads:** `Thread.print` → a full thread dump (stack of every thread, lock info) — the jstack replacement. - **Heap:** `GC.heap_dump <file>` → an HPROF heap dump; `GC.class_histogram` → per-class instance counts/bytes; `GC.run` → request a full GC — the jmap replacements. - **VM introspection:** `VM.flags` (all the -XX flags in effect), `VM.system_properties`, `VM.version`, `VM.command_line`, `VM.uptime` — the jinfo replacements. - **JFR (Java Flight Recorder):** `JFR.start`, `JFR.dump`, `JFR.stop`, `JFR.check` → start/stop/collect a low-overhead profiling recording. - **NMT (Native Memory Tracking):** `VM.native_memory` (when started with `-XX:NativeMemoryTracking=summary`). ## Bottom line jcmd is the *front door* to JVM diagnostics: discover JVMs (`jcmd`), discover commands (`jcmd <pid> help`), then run them. It is preferred over the scattered legacy tools because it is unified, safer, discoverable, and where new capabilities land.

  • How do you find out which commands a specific JVM supports?
    Run 'jcmd <pid> help' to list them, and 'jcmd <pid> help <command>' to see one command's arguments. The list is the live set the running JVM actually supports.
  • Can jcmd attach to a JVM running as a different user?
    Not normally. The attach mechanism requires the same OS user (or elevated privilege) and the same host; jcmd is a local tool, not remote.

saying these in an interview costs you the question

  • Thinking jcmd is a remote/monitoring tool — it attaches locally as the same user
  • Confusing jcmd with jconsole/VisualVM (GUIs) — jcmd is CLI
  • Believing it reads the target's memory directly rather than via the attach API
  • Claiming jmap/jstack are gone today — they still exist but are legacy/deprecated

context

open as a page

How do you capture a thread dump with jcmd, and what would you look for in the output when diagnosing a hang or high CPU?

level: middleimportance: must knowfreq 68%

basics

~10 s

Run 'jcmd <pid> Thread.print' to print every thread's stack. To diagnose a hang look for BLOCKED threads and deadlocks; for high CPU look for RUNNABLE threads stuck in the same code across several dumps.

open as a page

Compare GC.heap_dump, GC.class_histogram, and GC.run in jcmd. When would you reach for each, and what are the production cautions?

level: middleimportance: should knowfreq 60%

basics

~20 s

GC.class_histogram lists how many instances and bytes each class uses — a quick memory overview. GC.heap_dump writes a full snapshot file you analyze in a tool to find leaks. GC.run requests a garbage collection. All can pause the app, so use them carefully in production.

open as a page

What do jcmd's VM.flags and VM.system_properties tell you, and how would you use them to debug a misconfigured production JVM?

level: middleimportance: should knowfreq 45%

basics

~20 s

VM.flags shows the JVM's -XX options actually in effect (heap size, GC, etc.). VM.system_properties shows the Java system properties (like java.version or user-set -D values). Together they let you confirm what the running JVM is really configured with versus what you intended.

open as a page

How do you use jcmd to control Java Flight Recorder (JFR) on a running JVM, and why is JFR often preferred over ad-hoc heap/thread dumps for performance investigations?

level: seniorimportance: should knowfreq 52%

basics

~20 s

Use jcmd to start, check, dump, and stop a flight recording: 'JFR.start', 'JFR.check', 'JFR.dump filename=...', 'JFR.stop'. JFR records CPU, allocations, locks, and GC over time at very low overhead, so it captures continuous behavior that one-off dumps miss.

open as a page