Garbage Collection
The garbage collector (GC) serves as an automatic memory manager in the common language runtime (CLR). When a class object is created at runtime, certain memory space is allocated to it in the heap memory. However, after all the actions related to the object are completed in the program, the memory space allocated to it is a waste as it cannot be used. In this case, garbage collection is very useful as it automatically releases the memory space after it is no longer required.
Contents
Advantages of Garbage Collection
- You don’t need to free memory manually while developing your application.
- It also allocates objects on the managed heap efficiently.
- When objects are no longer used then it will reclaim those objects by clearing their memory, and keeps the memory available for future allocations.
- Managed objects automatically get clean content to start with, so their constructors do not have to initialize every data field.
When Garbage Collection Works
Garbage Collection occurs if at least one of multiple conditions is satisfied. These conditions are given as follows:
- When virtual memory is running out of space.
- When allocated memory is suppressed acceptable threshold (when GC found if the survival rate (living objects) is high, then it increases the threshold allocation).
- When we call GC.Collect() method explicitly, as GC runs continuously, we actually do not need to call this method.
Generations
Memory is divided into spaces called generations so it can handle long-lived and short-lived objects. Garbage collection primarily occurs with the reclamation of short-lived objects that typically occupy only a small part of the heap. There are three generations of objects on the heap:
Generation 0
This is the youngest generation and contains short-lived objects. An example of a short-lived object is a temporary variable. Garbage collection occurs most frequently in this generation.
Newly allocated objects form a new generation of objects and are implicitly generation 0 collections. However, if they are large objects, they go on the large object heap in a generation 2 collection.
Most objects are reclaimed for garbage collection in generation 0 and do not survive to the next generation.
Generation 1
This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.
Generation 2
This generation contains long-lived objects. An example of a long-lived object is an object in a server application that contains static data that's live for the duration of the process.
Generation 2 garbage collection is also known as a full garbage collection, because it reclaims all objects in all generations (that is, all objects in the managed heap).
Phases
A garbage collection has the following 3 phases:
- Marking Phase: A list of all the live objects is created during the marking phase. This is done by following the references from all the root objects. All of the objects that are not on the list of live objects are potentially deleted from the heap memory.
- Relocating Phase: The references of all the objects that were on the list of all the live objects are updated in the relocating phase so that they point to the new location where the objects will be relocated to in the compacting phase.
- Compacting Phase: The heap gets compacted in the compacting phase as the space occupied by the dead objects is released and the live objects remaining are moved. All the live objects that remain after the garbage collection are moved towards the older end of the
heap memory in their original order.