Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is the default garbage collector in JDK 21 and JDK 25, and how do you check which collector a running JVM is using?

In both JDK 21 and JDK 25, the default collector is G1 on server-class machines, and ergonomics picks Serial when the JVM sees a very small machine or container, roughly fewer than two CPUs or less than about 2 GB of memory. To check a running JVM, we read the startup GC log line, which says for example Using G1, run jcmd pid VM.flags to see which Use...GC flag is set, or look at the GarbageCollectorMXBean names over JMX, such as G1 Young Generation.

Why this question is asked

Knowing which collector a service runs sounds trivial, but it is one of the first things to check in any GC or latency investigation, and it is often not what people assume. A service tuned with G1 flags may actually be running Serial in a small pod, and the G1 flags are then silently ignored. Interviewers ask this to see whether we know the default and, more usefully, how to verify it. In this answer, we will look at the default and the rule behind it, then at three ways to check a JVM: the GC log, jcmd, and JMX.

1The Default Collector and the Rule Behind It

Since JDK 9 (JEP 248), G1 has been the default collector, and that is still true in JDK 21 and JDK 25. But default here means the choice ergonomics makes when we pass no GC flag. Ergonomics is the part of HotSpot that picks defaults from the hardware the JVM sees: number of CPUs, amount of memory and a few other facts.

The rule is that G1 is used on a server-class machine, which means at least two available processors and at least about 2 GB of memory. Otherwise, the JVM uses Serial, the simple single-threaded collector. Because the JVM is container-aware, available means what the container allows, not what the host has. A pod limited to 1 CPU, or to 1 GiB of memory, gets Serial. OpenJDK has proposed making G1 the default everywhere in a later release, so if you run something newer than JDK 25, check your version's release notes.

Step throughHow ergonomics picks the collector
  1. GC flag on the command line?
    YesUse that collectorNoLet ergonomics decide
  2. At least 2 available CPUs?container CPU limit counts
    YesCheck memoryNoUse Serial
  3. At least about 2 GB of memory?container memory limit counts
    YesUse G1NoUse Serial

3 steps in order. Play them, or step through them one at a time.

JDK 9 through JDK 25; the memory threshold is approximate.

2Check 1: The Startup GC Log Line

With unified logging enabled at the gc tag, the very first GC line names the collector. This is the easiest check, and it is already present in most services that log GC.

Shell
$ java -Xlog:gc -jar app.jar
[0.007s][info][gc] Using G1 // G1 selected
...
$ java -Xlog:gc -XX:+UseZGC -jar app.jar
[0.006s][info][gc] Using The Z Garbage Collector // ZGC
...
// Serial prints "Using Serial", Parallel prints "Using Parallel",
// Shenandoah prints "Using Shenandoah"

With -Xlog:gc*, several more lines follow at startup, under the gc,init tags in recent JDKs, listing heap region size, parallel and concurrent worker counts, and the maximum heap. Those lines also help explain why a JVM behaves differently in two environments.

3Check 2: jcmd on a Running Process

If a JVM is already running and we have shell access to the container, jcmd asks it directly. VM.flags lists the flags that were set, either by us or by ergonomics.

Shell
$ jcmd 4711 VM.flags
4711:
-XX:CICompilerCount=3 -XX:ConcGCThreads=1 -XX:G1ConcRefinementThreads=4
-XX:G1HeapRegionSize=2097152 -XX:InitialHeapSize=264241152 -XX:MaxHeapSize=4194304000
... -XX:+UseG1GC // the collector in use
$ jcmd 4711 VM.flags | tr ' ' '\n' | grep -E 'Use(Serial|Parallel|G1|Z|Shenandoah)GC'
-XX:+UseG1GC

Notice the other flags printed next to it: the region size, the heap sizes and the thread counts are all values ergonomics picked. On older processes we may also run jcmd pid GC.heap_info, which describes the heap layout in terms of the collector, for example garbage-first heap with region counts. And before starting a process, java -XX:+PrintFlagsFinal -version shows which collector the JVM would choose on the current machine.

4Check 3: GarbageCollectorMXBean Names Over JMX

Every collector registers one or more GarbageCollectorMXBean objects, and their names reveal which collector is running. This is how monitoring agents and Micrometer's JVM GC metrics find it, and we can read them from code.

Java
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
System.out.println(gc.getName() + " count=" + gc.getCollectionCount()
+ " timeMs=" + gc.getCollectionTime());
}
// With G1:
// G1 Young Generation count=12 timeMs=96
// G1 Concurrent GC count=2 timeMs=4 // added in recent JDKs
// G1 Old Generation count=0 timeMs=0 // full collections only
// With Serial: Copy ..., MarkSweepCompact ...
// With Parallel: PS Scavenge ..., PS MarkSweep ...
Collector names you will see
CollectorLog lineMXBean names
G1Using G1G1 Young Generation, G1 Old Generation
SerialUsing SerialCopy, MarkSweepCompact
ParallelUsing ParallelPS Scavenge, PS MarkSweep
ZGC (generational)Using The Z Garbage CollectorZGC Minor/Major Cycles and Pauses
ShenandoahUsing ShenandoahShenandoah Cycles, Shenandoah Pauses

ZGC registers beans with names such as ZGC Minor Cycles, ZGC Minor Pauses, ZGC Major Cycles and ZGC Major Pauses; the exact set changed when generational ZGC arrived, so check your version. Notice that G1 Old Generation counts only full collections, not mixed ones, which is a common source of confusion on dashboards.

5Follow-up questions

Why does it matter if a service runs Serial by accident?

Serial's pauses grow with live data because one thread does all the work, so a service with a 1.5 GB heap and a latency target can see much longer pauses than expected. Any G1 tuning flags, such as MaxGCPauseMillis, are then ignored without warning. Knowing the collector also tells us which log lines and metrics to read in an incident.

Can I change the collector of a running JVM?

No. The collector is chosen at startup and stays fixed for the life of the process. To change it, we change the flags or environment, for example JAVA_TOOL_OPTIONS or JDK_JAVA_OPTIONS, and restart.

Does Spring Boot Actuator show the collector?

Indirectly. The jvm.gc metrics exposed through Micrometer carry tags with the GC names from the MXBeans, such as G1 Young Generation, so the metric names in a dashboard tell us which collector is running.

6Conclusion

In this answer, we saw that G1 is the default collector in JDK 21 and JDK 25 on server-class machines, while ergonomics picks Serial when it sees fewer than two CPUs or less than about 2 GB, which happens in small containers. We checked the collector in three ways: the Using line at the start of -Xlog:gc, jcmd pid VM.flags showing UseG1GC, UseZGC, UseSerialGC or UseParallelGC, and the GarbageCollectorMXBean names over JMX, such as G1 Young Generation.

What to say in the interviewAbout 80 seconds aloud

Open with

In JDK 21 and JDK 25 the default is G1, but only on what the JVM considers a server-class machine, which is at least two available CPUs and roughly 2 GB of memory.

Then, if they want more

On anything smaller, and that includes containers with a one-CPU limit or a 1 GiB memory limit because the JVM reads the cgroup limits, ergonomics picks Serial instead. So I never assume; I check.

The simplest check is the first line of the GC log with -Xlog:gc, which says Using G1, Using Serial, Using Parallel or Using The Z Garbage Collector. On a running process I use jcmd with the pid and VM.flags, which lists the flags ergonomics chose, including UseG1GC, UseZGC, UseSerialGC or UseParallelGC, along with heap size and region size.

From code or a monitoring tool, the GarbageCollectorMXBean names tell me too: G1 Young Generation and G1 Old Generation for G1, Copy and MarkSweepCompact for Serial, PS Scavenge and PS MarkSweep for Parallel. That is also what Micrometer's GC metrics are tagged with, so dashboards show it.

It matters because a service tuned with G1 flags that actually runs Serial in a small pod silently ignores all of them.

About this answer

Last updated
12 November 2026
Versions it names
Java 25, Java 21, Java 9
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 "What is the default garbage collector in JDK 21 and JDK 25, and how do you check which...": https://scrivio-interview-library.netlify.app/java/garbage-collection/00499f1472/