Video in progress
When does an object move from the young generation to the old generation?
Each minor GC an object survives raises its age by one. When the age reaches the tenuring threshold (up to 15 by default, adjusted dynamically) or the survivor space runs out, the object is moved to the old generation.
📖 Answer
An object is born in eden. If it is still alive at a minor GC, it is copied to a survivor space and its age becomes 1. At each following GC it is copied between survivor spaces and its age grows. The age is stored in 4 bits in the object header, so the maximum is 15.
But the JVM changes this threshold dynamically: if the survivor space is more than a certain percentage full, even objects of a lower age are promoted to old early (premature promotion).
Why this matters: a short-lived object that lands in old turns into garbage there, and only the more expensive old/mixed collections clean it up. A typical case: a 50 MB temporary list inside a request, young is small, it doesn't fit in survivor and goes straight to old. As a result old fills up fast, and mixed GCs or even Full GCs become more frequent.
To see the age distribution:
-Xlog:gc+age=trace
The log shows how many bytes there are at each age. If many objects are going to old at ages 1-2, making young bigger usually helps (in G1, that means raising the overall heap or relaxing the pause target). With G1 it is better not to play with -Xmn by hand.
Sign up to mark studied questions and save your progress.