How many collections it take for .net garbage collector to move object to next generation

108 views Asked by At

I read that .net garbage collector moves objects to next generation if they survive collection. Is that true for every collection event? Then it means that object will be in last generation (2) after 2 collections in which it participates. Is that true?

I wonder, because in java case I read that it splits each generation to two chunks and to avoid fragmenting it uses alternately both of them and only after given number of collections objects become older. I can't find information about it for .net.

1

There are 1 answers

0
Konrad Kokosa On BEST ANSWER

Yes, this is almost always true - each GC means each surviving object promotion to an older generation. Thus, after two successive GCs, an object will land in the oldest generation (gen2). This is true for small objects (those which, by default, are smaller than 85000 bytes). So, in general, this is what one can assume.

There are special cases of objects not being promoted (we say they are "demoted") because of the pinning handling. Because of pinning, in the case of compaction, GC would introduce sometimes big gaps (fragmentation) if it insisted to always promote objects (including those pinned).

This might be illustrated by the following figure:

Example of demotion

A pinned object (dark grey) that lived in gen1 has been demoted to gen0 to make better use of space. In fact, due to the internal GC implementation, in such case, it is pinned object and one more, that are being demoted (which is shown above).