Reputation:
An object can be copied between survivor spaces 7 times before it is relocated into the Old Generation space and I understand this is so that the object has every chance of being garbage collected before being put into the old generation space. The lecture that I am listening to states that this is so to avoid object promotion from young generation space to old generation space.
My question is why is promotion to old generation bad? The lecture doesn't explain that.
Upvotes: 3
Views: 244
Reputation: 43042
Why is old generation bad?
The lecture that I am listening to states that this is so to avoid object promotion from young generation space to old generation space.
It's not that promotion to the old generation is bad in general. It's only bad when objects with a short lifetime get promoted by accident.
You want long-lived objects in the old generation so that they do not interfere with the minor collections.
Allocation rates are roughly inverse to object lifetimes (which gives rise to the weak generational hypothesis). Which in means short-lived objects are generated at a high rate. And that in turn means: If short-lived objects get promoted to the old gen then the old gen will by filled up at a high rate.
Minor GCs generally are less expensive than major GCs, especially in terms of pause times, which is why you want to run the latter less often.
With CMS and G1 you have to distinguish further. They try to collect the old generation in a partially concurrent manner - and in small chunks in G1's case - but they fall back on a full stop-the-world GC if they meet some failure cases or cannot keep up with the promotion rates.
Basically promoting too many short-lived objects too fast violates the weak generational hypothesis around which the GCs are constructed.
Upvotes: 3
Reputation: 718698
Because once an object is in old space, the only way it can be collected is by running a full garbage collection; i.e. garbage collecting the entire heap.
You want to avoid that.
Upvotes: 2