Skip to main content
PreparationZone Logo
PrepZone

Java 21 and What Comes Next

Generational ZGC, the preview features worth watching, and a practical 8 to 21 migration path.

Pass checkpoint below

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
Each long-term support release is supported for years, which is why most teams sit on one of these four rather than the six-month interim releases.

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 switch and 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

Java
-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
AspectChoose G1 whenChoose generational ZGC when
Pause targetTens of milliseconds is acceptablePauses must stay under a millisecond
Heap sizeAnything from a few hundred MB upwardLarge heaps — tens of GB and beyond
ThroughputSlightly higherA few percent lower in exchange for latency
Tuning-XX:MaxGCPauseMillis and little elseMostly self-tuning
Typical fitThe default for most servicesLatency-critical services, large caches
  • Pause target

    Choose G1 whenTens of milliseconds is acceptable
    Choose generational ZGC whenPauses must stay under a millisecond
  • Heap size

    Choose G1 whenAnything from a few hundred MB upward
    Choose generational ZGC whenLarge heaps — tens of GB and beyond
  • Throughput

    Choose G1 whenSlightly higher
    Choose generational ZGC whenA few percent lower in exchange for latency
  • Tuning

    Choose G1 when-XX:MaxGCPauseMillis and little else
    Choose generational ZGC whenMostly self-tuning
  • Typical fit

    Choose G1 whenThe default for most services
    Choose 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.

Java
# 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.