Find and inspect a JVM with jps and jcmd
35 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A Java service on a server is slow, and you cannot restart it without losing the problem. You need to find the process and ask it questions while it keeps running. Which tools do that?
jps: list the running JVMs
jps (JVM Process Status tool) lists the Java programs running on your computer, one per line, each with its process id. A process id, or pid, is the number the operating system gives every running program, so tools can point at exactly one of them.
Example 1 · A program to inspect: a results server that keeps every session
import java.util.ArrayList;import java.util.List;
class Session { final String rollNo; final byte[] marksheetPdf = new byte[20_000]; // about 20 KB per session
Session(String rollNo) { this.rollNo = rollNo; }}
public class ResultsServer { // Bug: a session is added for every visit but never removed static final List<Session> SESSIONS = new ArrayList<>();
static int servePage(int visit) { List<String> lines = new ArrayList<>(); // short-lived garbage for (int i = 0; i < 2000; i++) { lines.add("Roll " + visit + " subject " + i + " marks " + (i % 100)); } return lines.size(); }
public static void main(String[] args) throws InterruptedException { System.out.println("Results server started. Running for 90 seconds."); for (int second = 1; second <= 90; second++) { for (int visit = 0; visit < 50; visit++) { servePage(visit); SESSIONS.add(new Session("22CS" + (second * 100 + visit))); } Thread.sleep(1000); } System.out.println("Sessions kept: " + SESSIONS.size()); }}Results server started. Running for 90 seconds. Sessions kept: 4500
What this code does
- This is the program the tools in this module inspect. It runs for 90 seconds so you have time to type commands in a second terminal. It needs no flags and keeps only about 90 MB.
- Every second it serves 50 pages (short-lived garbage) and adds 50
Sessionobjects of about 20 KB each to a static list that is never cleared: a small, safe copy of an exam-results server whose memory grows all morning. - The output is labelled "Sample output" only because the program runs longer than the course checker allows; the two lines themselves are always the same.
Example 2 · Find the pid with jps
jpsjps -ljps -m28264 ResultsServer 25588 Jps 28264 ResultsServer 18484 jdk.jcmd/sun.tools.jps.Jps 28264 ResultsServer 28440 Jps -m
What this code does
- Each line is "pid name".
ResultsServerhas pid 28264 in this run (JDK 21 on Windows). Your pid will be different every time you start the program. - Lines for other Java programs (an IDE, other tools) were removed to keep the output short.
Jpsitself appears because it is also a Java program, and it gets a new pid on every call.
Example 3 · Print your own pid at startup
import java.lang.management.ManagementFactory;
public class MyPidDemo { public static void main(String[] args) { long pid = ProcessHandle.current().pid(); // Java 9 and later long samePid = ManagementFactory.getRuntimeMXBean().getPid(); // Java 10 and later
System.out.println("Fee portal started"); System.out.println("pid is a positive number: " + (pid > 0)); System.out.println("Both ways give the same pid: " + (pid == samePid)); System.out.println("Main class seen by jps: " + MyPidDemo.class.getName()); }}Fee portal started pid is a positive number: true Both ways give the same pid: true Main class seen by jps: MyPidDemo
What this code does
ProcessHandle.current().pid()(Java 9 and later) returns the pid of the running JVM. Many services print it in their first log line, so whoever reads the log can runjcmd <pid> ...straight away.ManagementFactory.getRuntimeMXBean().getPid()(Java 10 and later) gives the same number. The program printstrueinstead of the pid itself because the pid changes on every run.
jps alone prints the pid and the short main class name. jps -l prints the full class name (or the JAR path), jps -m adds the arguments passed to main, and jps -v adds the JVM options such as -Xmx256m. Your IDE, build tools and jps itself are Java programs too, so they show up in the list as well.
jps, jcmd, jstat and jmap come with the JDK (in its bin folder), not with a JRE-only runtime. On a server with only a JRE they are missing; install a JDK of the same version.jcmd: send a command to a running JVM
jcmd is the JDK tool that sends a diagnostic command, such as GC.heap_info or Thread.print, to a running JVM identified by its process id.
jcmd sends one diagnostic command to a running JVM: jcmd <pid> <command>. The JVM runs the command inside itself and prints the answer back to your terminal. The program does not restart and usually does not notice.
Example 4 · List JVMs with jcmd and ask one for help
jcmdjcmd <pid> help28292 ResultsServer 4132 GroupTurn 30296 jdk.jcmd/sun.tools.jcmd.JCmd 28264: The following commands are available: Compiler.CodeHeap_Analytics Compiler.codecache ... GC.class_histogram GC.finalizer_info GC.heap_dump GC.heap_info GC.run GC.run_finalization JFR.check JFR.configure JFR.dump JFR.start JFR.stop JFR.view ... Thread.dump_to_file Thread.print VM.cds VM.class_hierarchy VM.classes ... VM.flags VM.info ... VM.system_properties ... VM.uptime VM.version help For more information about a specific command use 'help <command>'.
What this code does
- Without arguments,
jcmdlists JVMs likejps -l. With a pid andhelp, the JVM itself replies with every command it supports. The first line,28264:, repeats which JVM answered. - The list was trimmed (
...); JDK 21 lists about 50 commands. The groups are easy to read:GC.*for the heap,Thread.*for threads,JFR.*for Flight Recorder (a later lesson) andVM.*for the JVM itself. - A wrong pid gives
java.io.IOException: no such process: check the pid withjpsagain, the program may have stopped.
jcmd with no arguments lists running JVMs, just like jps -l. jcmd <pid> help lists every command that JVM understands, and jcmd <pid> help GC.heap_dump explains one of them, including its options and an Impact line that warns how heavy the command is. You can also use the main class name instead of the pid: jcmd ResultsServer VM.version. Be careful: if two JVMs run the same class, the command goes to both of them and you get two answers.
jcmd attaches only to JVMs on the same machine, started by the same operating system user. On a Linux server you usually run it as the same user as the service, for example with sudo -u appuser jcmd <pid> ....VM.version, VM.flags and VM.system_properties
VM.version prints the exact JVM and JDK version of the running program. This matters because the java on your PATH may not be the one the server started with.
Example 5 · Which Java, which flags, which properties
jcmd <pid> VM.versionjcmd <pid> VM.flagsjcmd <pid> VM.command_linejcmd <pid> VM.system_properties28264: OpenJDK 64-Bit Server VM version 21.0.7+9-b895.130 JDK 21.0.7 28264: -XX:CICompilerCount=4 -XX:ConcGCThreads=3 -XX:G1ConcRefinementThreads=10 ... -XX:G1HeapRegionSize=1048576 ... -XX:InitialHeapSize=123731968 -XX:MarkStackSize=4194304 -XX:MaxHeapSize=1973420032 -XX:MaxNewSize=1183842304 -XX:MinHeapDeltaBytes=1048576 -XX:MinHeapSize=8388608 ... -XX:+UseCompressedOops -XX:+UseG1GC -XX:-UseLargePagesIndividualAllocation 28264: VM Arguments: java_command: ResultsServer java_class_path (initial): . Launcher Type: SUN_STANDARD 28264: #Thu Oct 15 23:33:46 IST 2026 file.encoding=UTF-8 ... java.class.version=65.0 ... java.version=21.0.7 java.vm.name=OpenJDK 64-Bit Server VM ... os.name=Windows 11 ... sun.java.command=ResultsServer ...
What this code does
- This run used a JetBrains build of OpenJDK 21.0.7 on Windows, started with plain
java ResultsServerand no flags. Long lines and lists were trimmed with.... VM.flagsstill shows-XX:+UseG1GCand-XX:MaxHeapSize=1973420032(1882 MB): the JVM picked G1 and about a quarter of this laptop's memory by itself. These chosen values are exactly what you need to know before reading any memory numbers.VM.system_propertiesalso prints paths, user names and settings. Treat its output as private when you paste it into a ticket or a chat.
VM.flags prints the JVM options in effect. It shows the flags you passed and also the ones the JVM chose for itself at startup, such as the garbage collector (-XX:+UseG1GC) and the maximum heap (-XX:MaxHeapSize, in bytes). VM.command_line shows the exact command that started the program. VM.system_properties prints Java system properties: java.version, java.home, user.dir (the working folder), file.encoding and any -Dname=value you passed. It answers "which Java, which folder, which settings" without asking anyone.
GC.heap_info and GC.class_histogram
GC.heap_info prints a short summary of the heap: which collector is used, how much heap is committed (total), how much is used, and how much metaspace the loaded classes take. It is cheap and safe to run often.
Example 6 · Heap summary and class histogram
jcmd <pid> GC.heap_infojcmd <pid> GC.class_histogram28264: garbage-first heap total 120832K, used 26643K [0x000000008a600000, 0x0000000100000000) region size 1024K, 22 young (22528K), 9 survivors (9216K) Metaspace used 336K, committed 512K, reserved 1114112K class space used 21K, committed 128K, reserved 1048576K 28264: num #instances #bytes class name (module) ------------------------------------------------------- 1: 10859 20520192 [B (java.base@21.0.7) 2: 9086 218064 java.lang.String (java.base@21.0.7) 3: 132 176240 [Ljdk.internal.vm.FillerElement; (java.base@21.0.7) 4: 1113 137472 java.lang.Class (java.base@21.0.7) 5: 1374 114984 [Ljava.lang.Object; (java.base@21.0.7) 6: 2736 87552 java.util.concurrent.ConcurrentHashMap$Node (java.base@21.0.7) 7: 2659 85088 java.util.HashMap$Node (java.base@21.0.7) 8: 1108 44320 java.util.LinkedHashMap$Entry (java.base@21.0.7) 9: 236 42656 [Ljava.util.HashMap$Node; (java.base@21.0.7) 10: 12 37720 [C (java.base@21.0.7) 11: 60 27328 [Ljava.util.concurrent.ConcurrentHashMap$Node; (java.base@21.0.7) 12: 1000 24000 Session ...
What this code does
GC.heap_infosays G1 ("garbage-first heap") has 120832 KB committed and 26643 KB used, split into 1 MB regions. Metaspace for the loaded classes is only 336 KB here.- The histogram, taken 20 seconds after start, shows 1000
Sessionobjects (24 bytes each) and about 20 MB ofbyte[]: the 20 KB marksheets the sessions hold. Classes from the JDK show their module, such asjava.base@21.0.7; your own classes have none. - The list continues for hundreds of lines and ends with a
Totalline; it was trimmed here. The Reading a heap dump lesson explains why a smallSessioncount can still hold most of the heap.
- The bytes column is the shallow size: only the objects of that class, not what they point to. A
Sessionobject is 24 bytes even though itsbyte[]field holds 20 KB. - By default
GC.class_histogramfirst runs a full GC so it counts only live objects. The help text marks it "Impact: High". On a busy server, run it when a short pause is acceptable. -allcounts unreachable objects too and skips that full GC.
GC.class_histogram counts the objects on the heap, grouped by class, sorted by bytes: rank, number of instances, total bytes and class name. It is the fastest way to see what fills the heap. Array classes use JVM names: [B is byte[], [C is char[], [I is int[] and [Ljava.lang.Object; is Object[].
Next step
Check your understanding
12 quick questions