What are the different generaions of Garbage Collection and
how do they work

Answer Posted / komilla shaheen

One feature of the garbage collector that exists purely to
improve performance is called generations. A generational
garbage collector takes into account two facts that have
been empirically observed in most programs in a variety of
languages:
1. Newly created objects tend to have short lives.
2. The older an object is, the longer it will survive.

Generational collectors group objects by age and collect
younger objects more often than older objects. When
initialized, the managed heap contains no objects. All new
objects added to the heap can be said to be in generation
0, until the heap gets filled up which invokes garbage
collection. As most objects are short-lived, only a small
percentage of young objects are likely to survive their
first collection. Once an object survives the first garbage
collection, it gets promoted to generation 1.Newer objects
after GC can then be said to be in generation 0.The garbage
collector gets invoked next only when the sub-heap of
generation 0 gets filled up. All objects in generation 1
that survive get compacted and promoted to generation 2.
All survivors in generation 0 also get compacted and
promoted to generation 1. Generation 0 then contains no
objects, but all newer objects after GC go into generation
0.

Thus, as objects "mature" (survive multiple garbage
collections) in their current generation, they are moved to
the next older generation. Generation 2 is the maximum
generation supported by the runtime's garbage collector.
When future collections occur, any surviving objects
currently in generation 2 simply stay in generation 2.

Thus, dividing the heap into generations of objects and
collecting and compacting younger generation objects
improves the efficiency of the basic underlying garbage
collection algorithm by reclaiming a significant amount of
space from the heap and also being faster than if the
collector had examined the objects in all generations.

A garbage collector that can perform generational
collections, each of which is guaranteed (or at least very
likely) to require less than a certain maximum amount of
time, can help make runtime suitable for real-time
environment and also prevent pauses that are noticeable to
the user.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do switch statements work?

474


Explain code compilation in c#.

518


Is c# 8 released?

478


What is Event - Delegate?

570


Can an abstract class have a constructor c#?

487






Explain the mechanism of VB.NET/C# achieve polymorphism?

531


What is _viewstart cshtml?

469


What is inumerable?

564


What is ulong in c#?

561


Is a valid int value?

470


How long can loop recorders stay in?

591


Are c# references the same as c++ references?

544


How does the clr work?

495


Why do we use methods in c#?

467


Is it true that all c# types derive from a common base class?

520