Generational Garbage Collection
Generational garbage collection (GC) divides the heap into age-based regions and collects recently allocated objects more frequently than long-lived objects. It relies on the common observation that many objects become unreachable soon after allocation.
A generational collector separates objects according to how long they have survived. In a typical Java heap, newly allocated objects begin in a young generation, while objects that survive enough collections can be promoted to an old generation. The exact regions and algorithms depend on the JVM garbage collector being used.
Young-generation collections are usually frequent because the young generation is relatively small and often contains many unreachable objects. Collecting this area can therefore reclaim substantial memory without examining the entire heap every time. Objects that remain reachable are retained or moved within the young-generation areas according to the collector's design.
Objects that survive repeated young collections may be promoted to the old generation. The old generation is generally collected less frequently because long-lived objects are less likely to become unreachable between successive collections. This reduces the amount of work required compared with treating every object as equally old.
Generational collection introduces an important complication: an old object can reference a young object. The collector must preserve such references when determining which young objects are reachable. JVM implementations use mechanisms such as remembered sets, card tables, or write barriers to track relevant cross-generation references.
Generational GC improves efficiency when the workload has strong object-age locality, but it is not a guarantee of low pause times or low memory usage. Collector choice, heap sizing, allocation rate, object lifetimes, promotion behavior, and application workload all affect actual GC performance.
Key points
- New objects are typically allocated in a young generation.
- Objects surviving enough collections may be promoted to an old generation.
- Young collections can happen more frequently because short-lived objects are common.
- Cross-generation references must be tracked so reachable young objects are not incorrectly reclaimed.
- Generational GC exploits object lifetime patterns; it does not guarantee a particular pause time.
Interview questions
What happens to an object that survives a young-generation collection?
A surviving object remains reachable and must be preserved. Depending on the collector and its policies, it may be copied or otherwise relocated within the young-generation area. After surviving enough collections, it may be promoted to an old-generation area. The exact promotion mechanism and threshold are implementation-dependent.
Why does generational GC improve garbage-collection efficiency?
Many applications create objects that become unreachable relatively soon after allocation. By collecting the young generation frequently, the JVM can reclaim these objects without repeatedly scanning all long-lived objects. Objects that survive several collections can be treated as longer-lived and collected less often. This reduces unnecessary work when the application's allocation pattern has strong generational locality.
Why are write barriers or remembered sets needed in a generational collector?
A young collection needs to determine whether young objects are reachable. An old object may hold a reference to a young object, so examining only roots and young objects is not sufficient. Write barriers can record relevant reference changes, and remembered sets or related structures can identify old-to-young references that must be considered. This lets the collector avoid scanning the entire old generation for every young collection.
What is the difference between a young collection and a full GC?
A young collection focuses primarily on the young portion of the heap and the references needed to determine its reachability. A full GC generally involves a much broader heap scope and may include old-generation objects and other runtime-managed areas, depending on the JVM and collector. Because more memory may need to be processed, a full collection can require substantially more work. The exact terminology and behavior depend on the garbage collector.
What signs might indicate excessive promotion or GC pressure in a Java service?
Useful signals include high allocation rates, frequent young collections, increasing promotion into the old generation, and old-generation collections occurring more often than expected. GC logs and JVM metrics can show pause durations, allocation behavior, survivor occupancy, and promotion patterns. The diagnosis should also consider heap sizing, application allocation patterns, and the selected collector rather than assuming that promotion alone is the root cause. A production investigation should correlate GC behavior with application latency and throughput.