Garbage collection is a .Net feature to free the unused code objects in the memory.
The memory heap is divided into three generations. Generation 0, Generation 1 and Generation 2.
Generation 0 – This is used to store short-lived objects. Garbage Collection happens frequently in this Generation.
Generation 1 – This is for medium-lived objects. Usually, the objects that get moved from generation 0 are stored in this.
Generation 2 – This is for long-lived objects.
Collecting a Generation refers to collecting the objects in that generation and all its younger generations. Garbage collection of Generation 2 means full garbage collection, it collects all the objects in Generation 2 as well as Generation 1 and Generation 0.
During the Garbage collection process, as the first phase, the list of live objects is identified. In the second phase, references are updated for those objects which will be compacted. And in the last phase, the space occupied by dead objects are reclaimed. The remaining objects are moved to an older segment.