Why this matters
- Knowing which features are final and which are previews is the difference between a confident upgrade and an unpleasant surprise two releases later.
- Garbage collection improved more between 8 and 21 than in the decade before, and the right collector choice is usually a configuration change rather than a code change.
- A migration plan that sequences the work correctly takes days; one that does everything at once takes months.
Java 8Lambdas, streams
Java 11var, HTTP client
Java 17Records, sealed, switch
Java 21Virtual threads, patterns
What is final in Java 21
Production-ready, no flags required
- Virtual threads — thread-per-request at a scale that previously needed reactive code.
- Pattern matching for
switchand record patterns — exhaustive, destructuring dispatch over sealed hierarchies. - Sequenced collections — one vocabulary for first, last and reversed.
- Generational ZGC — sub-millisecond pauses with much better throughput than non-generational ZGC.
- A key encapsulation mechanism API for modern cryptography.
- Plus everything final in 17: records, sealed classes, text blocks, switch expressions, helpful NPE messages.
Garbage collection in 21
-XX:+UseG1GC # the default since Java 9 — balanced, tunable, good enough
-XX:+UseZGC -XX:+ZGenerational # sub-millisecond pauses, generational since 21
-XX:+UseShenandoahGC # low pause, an alternative to ZGC
-XX:+UseSerialGC # single-threaded — genuinely best for small containers
-XX:+UseParallelGC # throughput-first, when pause length does not matter
| Aspect | Choose G1 when | Choose generational ZGC when |
|---|---|---|
| Pause target | Tens of milliseconds is acceptable | Pauses must stay under a millisecond |
| Heap size | Anything from a few hundred MB upward | Large heaps — tens of GB and beyond |
| Throughput | Slightly higher | A few percent lower in exchange for latency |
| Tuning | -XX:MaxGCPauseMillis and little else | Mostly self-tuning |
| Typical fit | The default for most services | Latency-critical services, large caches |
Pause target
Choose G1 whenTens of milliseconds is acceptableChoose generational ZGC whenPauses must stay under a millisecondHeap size
Choose G1 whenAnything from a few hundred MB upwardChoose generational ZGC whenLarge heaps — tens of GB and beyondThroughput
Choose G1 whenSlightly higherChoose generational ZGC whenA few percent lower in exchange for latencyTuning
Choose G1 when-XX:MaxGCPauseMillis and little elseChoose generational ZGC whenMostly self-tuningTypical fit
Choose G1 whenThe default for most servicesChoose generational ZGC whenLatency-critical services, large caches
Generational ZGC in 21 removed the main reason not to use ZGC — it now handles high allocation rates well.
# Measure before you tune — unified logging makes this easy
-Xlog:gc*:file=gc.log:time,uptime,level,tags
# Containers: let the JVM see the cgroup limits (default since Java 10)
-XX:MaxRAMPercentage=75
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 5 sections
- The previews worth understanding
- A migration path from 8 to 21
- What to expect from the upgrade
- Staying current after 21
- Common misreadings
Test yourself
Answer these before moving on — recall is what makes it stick.