Java / Java 21 Coding Standards Interview Questions
Explain the internal working of Generational ZGC and its influence on object-lifecycle coding standards?
Generational ZGC (finalized as the default ZGC mode in Java 21) splits the heap into a young generation for newly allocated objects and an old generation for objects that survive multiple collections, based on the well-established observation that most objects die young.
flowchart LR
A[Object allocated] --> B[Young generation]
B -->|survives a collection| C[Promoted to old generation]
B -->|dies young, most objects| D[Reclaimed quickly]
C -->|collected far less frequently| E[Long-lived object]
Because the young generation is collected far more frequently and cheaply than the old generation, and both collections happen concurrently with the application's own threads with only sub-millisecond pauses, Generational ZGC rewards code that allocates short-lived objects freely rather than fights against garbage collection with manual object pooling.
The coding-standard implication is a shift away from older advice to minimize allocation or reuse mutable objects for performance: freely creating short-lived, immutable objects - including records, streams, and enhanced-switch temporaries - now plays directly to the generational collector's strength, while long-lived caches of mutable state are what actually deserve scrutiny, since it is old-generation objects that are more expensive to collect.
More Related questions...