Quick revision
The short answer
On JDK 21 we enable ZGC with -XX:+UseZGC and add -XX:+ZGenerational to get the generational mode; without it, JDK 21 runs the older non-generational ZGC. Generational mode became the default in JDK 23 and the only mode from JDK 24, so on JDK 25 -XX:+UseZGC alone gives generational ZGC. We choose ZGC over G1 for large heaps or strict tail latency targets where G1 pauses of tens or hundreds of milliseconds are too long, and accept higher CPU use and more memory headroom in return.
What to remember
In this answer, we saw that JDK 21 needs -XX:+UseZGC plus -XX:+ZGenerational for generational ZGC, while from JDK 23 generational mode is the default and from JDK 24 the only mode, so on JDK 25 -XX:+UseZGC is all we need and ZGenerational should be removed. We choose ZGC over G1 for large heaps and tail latency targets that G1's pauses of tens or hundreds of milliseconds would break, and we pay for it with more CPU, load barrier overhead and more heap headroom.
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.
On JDK 21 we enable ZGC with -XX:+UseZGC and add -XX:+ZGenerational to get the generational mode; without it, JDK 21 runs the older non-generational ZGC. Generational mode became the default in JDK 23 and the only mode from JDK 24, so on JDK 25 -XX:+UseZGC alone gives generational ZGC. We choose ZGC over G1 for large heaps or strict tail latency targets where G1 pauses of tens or hundreds of milliseconds are too long, and accept higher CPU use and more memory headroom in return.
Why this question is asked
ZGC's flags changed across three releases in a row, and many teams upgraded from JDK 21 to 25 with their old flags still in place. Some got a warning, some got the wrong mode on 21 without knowing it. Interviewers ask this to check that we know the current state rather than a blog post from 2022, and that we can explain when ZGC is worth it. In this answer, we will recap what ZGC is, compare the flags on JDK 21 and JDK 25, see how to confirm the mode, and then look at when to choose ZGC over G1 and what it costs.
1A Quick Recap of ZGC
ZGC is a low-latency collector in OpenJDK. It does almost all its work, marking live objects, moving them and fixing references, concurrently with the application. Its stop-the-world pauses only synchronise between phases and scan a small set of roots, so they stay well under a millisecond regardless of heap size, from a few hundred megabytes up to terabytes.
The first versions of ZGC were not generational: every cycle marked the whole heap. That works, but for most applications most objects die young, and collecting them in a cheap young generation is far more efficient. Generational ZGC, introduced in JDK 21 by JEP 439, splits the heap into young and old generations and collects the young one more often.
2The Flags on JDK 21
JDK 21 shipped both modes, with the old one as the default, so we had to ask for generational mode explicitly.
Notice how easy it is to get this wrong: a team that only adds -XX:+UseZGC on JDK 21 runs the older mode, which usually needs more CPU and headroom for the same workload. If we are on JDK 21 and use ZGC, the second line is the one to use.
3What Changed in JDK 23, 24 and 25
In JDK 23, JEP 474 made generational mode the default for ZGC, so -XX:+UseZGC alone gives generational ZGC and -XX:+ZGenerational became redundant and deprecated. In JDK 24, JEP 490 removed the non-generational mode entirely. On JDK 25, the current LTS, there is only one ZGC, and it is generational.
| JDK | -XX:+UseZGC alone gives | -XX:+ZGenerational |
|---|---|---|
| 21 | non-generational ZGC | needed for generational mode |
| 23 | generational ZGC (JEP 474) | redundant, deprecated |
| 24 | generational, the only mode (JEP 490) | obsolete, warning |
| 25 LTS | generational, the only mode | remove from flags |
The exact warning text depends on the release, so check your version, but the rule for upgrades is simple: when moving from 21 to 25, remove ZGenerational from the flags.
We can confirm what is running from the startup log. With -Xlog:gc, ZGC prints its mode at startup, and on JDK 25 the line reads along the lines of Using The Z Garbage Collector; jcmd pid VM.flags shows UseZGC and, on 21, whether ZGenerational is set.
4When to Choose ZGC Over G1
G1 is a good default. On a 4 GB heap with a sensible pause target, its young pauses are typically 10 to 50 ms. But G1 pauses grow with the amount of live data each pause has to copy, and mixed or remark pauses on a large heap can reach hundreds of milliseconds. If a service's tail latency target is close to or below those numbers, G1 cannot hold it reliably.
So ZGC fits services with large heaps, tens to hundreds of gigabytes, such as in-memory caches, search indexes or analytics engines, where G1 pauses grow; and services with strict tail latency targets, such as a p99.9 of 10 ms for a trading gateway or an ad server. For a typical REST service with a 2 GB heap and a 200 ms p99 target, G1 is usually fine and cheaper.
5What ZGC Costs
Concurrent collection is not free. ZGC's GC threads run alongside the application, so a service typically spends more total CPU on GC than with G1; a few percent more is common, more under heavy allocation. Every reference load also goes through a load barrier, a small check the JIT inserts, which adds some overhead to application code.
ZGC also needs memory headroom. Because the application keeps allocating while a cycle runs, the heap must have room for those allocations until the cycle frees memory. If the headroom runs out, threads stall waiting for memory, which looks like a latency spike rather than a GC pause. ZGC also does not use compressed object pointers, so object references take 8 bytes instead of 4, which makes the same live set somewhat larger than under G1. A service moving from G1 to ZGC therefore usually needs a larger heap, and we should measure CPU, allocation stalls and latency on a canary before switching.
6Follow-up questions
Is ZGC production-ready on JDK 21?
Yes. ZGC has been a product feature since JDK 15 (JEP 377), and generational ZGC was a product feature from its first release in JDK 21, just not the default mode. Many teams run generational ZGC on 21 successfully. The main reason to prefer 25 is that the remaining defaults and improvements are newer, not that 21 is unsafe.
Does ZGC need any tuning flags?
Usually only the maximum heap size, -Xmx, and perhaps SoftMaxHeapSize. ZGC sizes its threads and young generation automatically. The most important decision is giving it enough heap headroom, since too little leads to allocation stalls.
What happens if a JDK 25 service still has old G1 flags next to UseZGC?
Flags that belong to G1, such as G1HeapRegionSize or InitiatingHeapOccupancyPercent, are simply not used by ZGC. They do no harm, but they mislead the next person reading the configuration, so remove them when switching collectors.
7Conclusion
In this answer, we saw that JDK 21 needs -XX:+UseZGC plus -XX:+ZGenerational for generational ZGC, while from JDK 23 generational mode is the default and from JDK 24 the only mode, so on JDK 25 -XX:+UseZGC is all we need and ZGenerational should be removed. We choose ZGC over G1 for large heaps and tail latency targets that G1's pauses of tens or hundreds of milliseconds would break, and we pay for it with more CPU, load barrier overhead and more heap headroom.
Open with
On JDK 21 I enable ZGC with -XX:+UseZGC, but that alone gives the older non-generational mode, so I also add -XX:+ZGenerational to get generational ZGC, which is what I want almost always.
Then, if they want more
In JDK 23 generational mode became the default, and in JDK 24 the non-generational mode was removed, so on JDK 25 -XX:+UseZGC alone gives generational ZGC, and the ZGenerational flag only produces a warning and should be removed on upgrade. I confirm the mode from the startup line of -Xlog:gc or with jcmd VM.flags.
I would choose ZGC over G1 when the heap is large, tens or hundreds of gigabytes, or when the tail latency target is strict, say a p99.9 of a few milliseconds, because G1's pauses grow with the live data it copies and can reach tens or hundreds of milliseconds, while ZGC's pauses stay under a millisecond regardless of heap size.
The price is more CPU for concurrent GC threads, a load barrier on reference loads, no compressed oops, and more heap headroom, because the application keeps allocating during a cycle and runs into allocation stalls if there is not enough room. So I would trial it on a canary and compare latency, CPU and stalls against G1.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 25, Java 23, Java 24
- 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 "How do you enable ZGC on JDK 21 compared with JDK 25, and what are typical reasons to...": https://scrivio-interview-library.netlify.app/java/garbage-collection/642ecbceda/