Why does too long waiting for the promotion of an object from the younger generation lead to a decrease in efficiency?

Why does waiting too long for the promotion of an object from the younger generation lead to inefficiency?

One explanation I have seen is that if you wait too long for the young generation to advance, it will become less rare (i.e. the size of the living set will get larger when viewed as a percentage of the total size of the young generation).

But how does this lead to reduced efficiency? Suppose that when the young generation is rare (say, a living set is 2% of its size), collecting it will take X

CPU time, and 50 collections over a period of time T

will take 50X

CPU time. If instead we wait until it fills up to 100% of its size (which will also take a period T

), collecting all of it at once should only take proportionally longer, i.e. (100/2)*X=50X

processor time. So the total time taken to collect will be the same. Whether you collect 50 times at 2% occupancy or collect once at 100% occupancy. Where is the effectiveness advantage of the first approach?

+3


source to share


2 answers


Copying.

Most of the objects in the younger generation die and don't touch GC at all (yes, it's actually a survivor collector, live objects are copied and the rest is free memory).

The object preserved in several small collections is likely to have a long life. So copying it over and over again would be a waste of time.

if instead we wait until it fills up to 100% of its size



You don't have this option. You collect whenever the eden space becomes (almost) full. You don't do anything if only 2% is used. But...

After collecting, you will see that perhaps only 2% of the object remains.

Your mistake is misinterpreting the percentage. This 2% is not a threshold, it is a result, and you do not have a button to change it. You can change the size of eden and things like that, but the impact on the survivor percentage is just indirect.

+2


source


One reason is that every new generation object will be copied from eden to survivor, and then multiple times between survivors. Therefore, if you know that a copy will not die, young promotion on it will quickly reduce the number of copies. If the object is in the old generation, it usually will not be copied (only passed). This not only reduces the number of copy operations, but also the need to change the instance address in all links.

In the younger generation, there is no fragmentation problem with the copy collector, so this is not the reason.



It should be noted, however, that it is not trivial for an object to die young or not. If you have very short transactions, you can assume that most objects die quickly, so there is no need to store them in survivor locations (at all).

+1


source







All Articles