Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Which jcmd commands would you run first on a misbehaving JVM, and what does each tell you? Why is jcmd generally preferred over jstack and jmap today?

On a misbehaving JVM, we start with jcmd <pid> help to see what that JVM supports, then VM.version and VM.flags for the runtime and its settings, Thread.print for a thread dump with lock information, and GC.heap_info and GC.class_histogram for memory. From there we go deeper with GC.heap_dump for a full heap dump, JFR.start and JFR.dump for a flight recording, and VM.native_memory if Native Memory Tracking is enabled. jcmd is preferred because it is one tool for all of these through the attach API, while jstack and jmap are older single-purpose tools; in every case it must run as the same OS user as the target JVM and, in a container, see the target's process.

Why this question is asked

When a Java service is slow, stuck or eating memory, the first minutes decide whether we find the cause or just restart and hope. jcmd is the Swiss army knife for those minutes, and knowing which commands to run, in what order and what each costs, is a basic production skill. Interviewers ask this to see whether we have a routine, and whether we know that some commands are cheap and others pause the service. In this answer, we will see how jcmd works, which commands to run first and what they tell us, which ones to use carefully, why jcmd replaced jstack and jmap, and the practical requirements for using it in containers.

1How jcmd Talks to a JVM

jcmd is a command-line tool that ships with the JDK. It uses the attach API, a mechanism built into HotSpot: the tool signals the target JVM, the JVM starts a small attach listener thread, and the two communicate through a socket file in the target's temporary directory. The tool sends a diagnostic command by name, the JVM runs it inside its own process, and the output comes back to the terminal. Because the command runs inside the target, the result reflects the JVM's own view, and the set of available commands depends on the target's version.

We find the process id with jcmd -l, which lists running Java processes, and then pass the id with every command.

2The First Commands and What They Tell Us

A good routine starts with cheap commands that do not disturb the service. help shows which commands this JVM supports, VM.version tells us exactly which runtime we are dealing with, and VM.flags shows the effective non-default settings, such as heap size and collector. Then Thread.print gives a thread dump: every thread's state and stack, with the locks each thread holds and waits for, which is what we need for a stuck or slow service. GC.heap_info summarises how full the heap and its generations or regions are.

Shell
jcmd -l # 4711 com.acme.Orders
jcmd 4711 help # lists the commands this JVM supports
jcmd 4711 VM.version # OpenJDK 64-Bit Server VM version 21.0.5+11
jcmd 4711 VM.flags # -XX:MaxHeapSize=2147483648 -XX:+UseG1GC ...
jcmd 4711 Thread.print -l > threads-1.txt # thread dump with lock details
jcmd 4711 GC.heap_info
# garbage-first heap total 2097152K, used 1843200K
# region size 1024K, 310 young (317440K), 12 survivors (12288K)

In two minutes this tells us what is running, how it is configured, what every thread is doing, and whether memory is nearly full. Taking two or three thread dumps a few seconds apart is a good habit: a thread that is stuck in the same place in every dump is a real suspect, while one that moves is just busy.

3Going Deeper: Histograms, Heap Dumps, JFR and NMT

If memory looks like the problem, GC.class_histogram lists, for every class, how many instances exist and how many bytes they take. It is much smaller than a heap dump and often enough to spot a growing type, such as a million HashMap$Node objects. Notice that it forces a full GC by default to count only live objects, so it causes a pause.

GC.heap_dump writes the complete heap to a file for analysis in a tool such as Eclipse MAT. It pauses the JVM for a time that grows with the heap size and creates a file about as large as the used heap, and the file contains all data in memory, so it must be handled as sensitive. JFR.start begins a Java Flight Recorder recording, a low-overhead event log of GC, locks, I/O, allocation and CPU samples, and JFR.dump writes it to a file. VM.native_memory reports memory used outside the heap, but only if the JVM was started with -XX:NativeMemoryTracking=summary or detail.

Shell
jcmd 4711 GC.class_histogram | head -5
# num #instances #bytes class name
# 1: 4123987 98975688 [B
# 2: 3911034 93864816 java.lang.String
# 3: 1203112 38499584 java.util.HashMap$Node
jcmd 4711 JFR.start name=diag duration=120s filename=/tmp/diag.jfr
jcmd 4711 GC.heap_dump /dumps/orders-4711.hprof # pause plus a file the size of the used heap
jcmd 4711 VM.native_memory summary # only with -XX:NativeMemoryTracking enabled

4Which Commands Are Safe on a Busy Service

The commands differ a lot in cost, and in production that matters as much as what they show.

jcmd commands by cost
CommandTells usCost
help, VM.version, VM.flagsruntime and settingsnegligible
VM.command_line, VM.system_propertiesstartup arguments, propertiesnegligible
Thread.printthread states, stacks, locksshort safepoint pause
GC.heap_infoheap occupancynegligible
GC.class_histograminstances and bytes per classfull GC pause
JFR.start / JFR.dumpevents over timeabout 1 percent while recording
GC.heap_dumpevery objectlong pause, large sensitive file
VM.native_memorynon-heap memoryneeds NMT enabled at startup

5Why jcmd Instead of jstack and jmap

jstack prints thread dumps and jmap prints histograms and writes heap dumps. They still work, and many runbooks mention them. But each does one job, their options differ, and some older modes attached in a more intrusive way. jcmd offers the same capabilities, plus JFR control, native memory, flags, system properties and much more, through one consistent interface on top of the attach API. The JDK documentation itself recommends jcmd for new use. Learning one tool also means one set of permissions and one routine in runbooks.

The equivalents are direct: jstack -l <pid> corresponds to jcmd <pid> Thread.print -l, jmap -histo:live <pid> to GC.class_histogram, and jmap -dump:live,format=b,file=x <pid> to GC.heap_dump x.

6Practical Requirements: Same User, Same Process View

The attach API has security rules. jcmd must run as the same operating system user as the target JVM, otherwise the attach fails, typically with an error that the target is not responding or that permission is denied. When we log in as a different user, we switch to the JVM's user first, for example with sudo -u.

In containers, jcmd must see the target process and its temporary directory. The usual way is to run it inside the same container with kubectl exec, which only works if the image contains a JDK with jcmd. Otherwise we use an ephemeral debug container that shares the process namespace. And because jcmd runs in its own JVM, a JAVA_TOOL_OPTIONS variable in the container also applies to jcmd itself, which occasionally causes confusing errors; unsetting it for the jcmd call avoids that.

7Follow-up questions

What do I do if jcmd cannot attach?

Check that you are the same user as the target, that /tmp is the same directory the target uses, and that the JVM was not started with -XX:+DisableAttachMechanism. As a fallback for a thread dump, sending SIGQUIT with kill -3 makes the JVM print a dump to its standard output without attaching.

Is Thread.print safe to run on a busy production service?

Generally yes. It needs a brief safepoint, a moment when all Java threads stop, which takes milliseconds for most services. A heap dump or a class histogram is far more disruptive.

Do I need to restart the JVM to use JFR?

No. JFR.start begins a recording on a running JVM. Only Native Memory Tracking must be enabled at startup, because it has to track allocations from the beginning.

8Conclusion

In this answer, we saw that jcmd sends diagnostic commands into a running JVM through the attach API. We start with help, VM.version and VM.flags, take a thread dump with Thread.print and check GC.heap_info, then go deeper with GC.class_histogram, GC.heap_dump, JFR.start and JFR.dump, and VM.native_memory when NMT is on, keeping their costs in mind. jcmd replaced jstack and jmap as one tool for all of this, and it must run as the same user and see the target's process.

What to say in the interviewAbout 90 seconds aloud

Open with

My first run on a misbehaving JVM is jcmd. jcmd -l to find the pid, then help, because the available commands depend on that JVM's version. VM.version tells me exactly what runtime it is, and VM.flags the effective settings like heap and collector.

Then, if they want more

Then Thread.print with -l for lock details, two or three times a few seconds apart, because a thread stuck in the same place in every dump is a real suspect. GC.heap_info shows how full the heap is. If memory looks wrong, GC.class_histogram gives instance counts and bytes per class, but it triggers a full GC, so it pauses.

GC.heap_dump writes the whole heap for MAT, which means a pause that grows with heap size, a file as big as the used heap, and data I have to treat as sensitive. JFR.start and JFR.dump give a low-overhead recording of GC, locks, I/O and CPU samples, and VM.native_memory shows non-heap memory if NMT was enabled at startup.

I prefer jcmd because it's one tool for all of this over the attach API, with a consistent interface, while jstack and jmap are older single-purpose tools that still work.

Two practical rules: jcmd has to run as the same OS user as the target, and in a container it has to see the target process, so I run it in the same container or in a debug container that shares the process namespace.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Which jcmd commands would you run first on a misbehaving JVM, and what does each tell...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/4890090b71/