Detailed explanation
In a generational JVM garbage collector, the heap is logically divided into the Young Generation and the Old Generation, also called Tenured. This design uses the generational hypothesis: most objects become unreachable quickly, while objects that survive several collections often remain useful longer. Collecting recently allocated objects separately can reclaim substantial memory without repeatedly scanning the entire heap. However, the exact layout depends on the garbage collector; not every JVM collector uses the traditional Eden-and-Survivor arrangement.
In the traditional layout, the Young Generation contains Eden and two Survivor spaces. Most ordinary objects are initially allocated in Eden, often through a thread-local allocation buffer, or TLAB, which makes allocation efficient. During a young collection, reachable objects from Eden and the active Survivor space are usually copied into the other Survivor space, while unreachable objects require no copying. The Survivor spaces then exchange roles. Surviving objects accumulate age and may be promoted to the Old Generation when they reach a collector-controlled threshold or when Survivor capacity is insufficient. Promotion therefore does not happen after a universally fixed number of collections.
The Old Generation contains objects promoted from the Young Generation, although some allocation paths place objects directly into old-generation storage. Reclaiming old objects can involve more work, so collectors generally avoid processing the entire old generation on every young collection. A Minor GC commonly means a young-generation collection. Major GC is less precise and should be interpreted using the collector's documentation or logs; it is not automatically synonymous with Full GC. A Full GC generally involves the whole heap. Collectors use mechanisms such as card tables and remembered sets to track references from old objects into young objects.
Metaspace is not a heap generation: since Java 8, it stores class metadata in native memory, replacing PermGen. Its metadata can be reclaimed through class unloading. Interviewers expect this distinction and awareness of collector differences. For example, G1 assigns heap regions to young or old roles rather than using fixed contiguous generation areas, and its mixed collections reclaim selected old regions alongside young regions.
Key points to remember
- Generational collectors separate short-lived objects in Young Generation from longer-lived objects in Old Generation.
- The traditional Young Generation consists of Eden and two alternating Survivor spaces.
- Promotion depends on object age, collector policy, and available Survivor capacity.
- Minor GC usually collects young objects; Major GC and Full GC should not be treated as interchangeable terms.
- Metaspace stores class metadata outside the Java heap; heap organization varies by collector.
Common mistakes candidates make
- Calling Metaspace the third JVM heap generation or saying it stores ordinary Java objects.
- Claiming every object starts in Eden and is promoted after a fixed number of GC cycles.
- Assuming every collector uses two physical Survivor spaces or that every old-generation collection is a Full GC.
Likely follow-up questions
Why are two Survivor spaces used in the traditional layout?
They support copying collection: live young objects are copied into the destination Survivor space, leaving reclaimed space behind. Their roles alternate after a collection, and objects that cannot remain in Survivor space may be promoted.
How can a young collection avoid scanning the entire Old Generation?
Write barriers record relevant reference updates using structures such as card tables or remembered sets. The collector consults these records to find references into young objects without scanning every old object.
How does G1 organize generations differently?
G1 divides the heap into regions that can serve as Eden, Survivor, or old regions, with their roles changing over time. Young collections process young regions, while mixed collections also include selected old regions.
Interview tip: Explain the traditional object lifecycle first, then clarify that Metaspace is outside the heap and collector-specific layouts and GC terminology vary.
