Quick revision
The short answer
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.
What to remember
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.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
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.
- GC flag on the command line?YesUse that collectorNoLet ergonomics decide
- At least 2 available CPUs?container CPU limit countsYesCheck memoryNoUse Serial
- At least about 2 GB of memory?container memory limit countsYesUse 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.
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.
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.
| Collector | Log line | MXBean names |
|---|---|---|
| G1 | Using G1 | G1 Young Generation, G1 Old Generation |
| Serial | Using Serial | Copy, MarkSweepCompact |
| Parallel | Using Parallel | PS Scavenge, PS MarkSweep |
| ZGC (generational) | Using The Z Garbage Collector | ZGC Minor/Major Cycles and Pauses |
| Shenandoah | Using Shenandoah | Shenandoah 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.
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.
- Official documentation
- HotSpot Garbage Collection Tuning Guide, JDK 21
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/