Video in progress
What is the purpose of splitting the heap into young and old generations?
It is based on the observation that most objects "die" very quickly. New objects are born in the young generation and are cleaned often and cheaply; the long-lived ones are moved to the old generation and checked less often.
📖 Answer
This is called the "generational hypothesis": a DTO, a StringBuilder and iterators created during a web request become useless within milliseconds. A cache or beans live as long as the application.
The young generation consists of Eden and two Survivor areas (S0, S1). An object created with new lands in Eden.
The process:
- Eden fills up -> minor GC: live objects are copied to a Survivor area, and Eden is emptied completely.
- With each copy, the object's "age" increases.
- When the age passes the threshold (
MaxTenuringThreshold, up to 15), it is moved to the old generation (promotion).
The advantage: a minor GC copies only the live objects. Since most are dead, this is very fast. The old generation is large and changes rarely, so it doesn't need frequent checks.
G1 is generational too, only young/old are not one contiguous block but a set of regions.
Sign up to mark studied questions and save your progress.