skip to content

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