C# – Understanding The NET Garbage Collector
A key pillar of the .NET framework is the managed code environment in which an in-built garbage collector automatically handles the tasks related to disposing of unused objects to free up memory. While the process is automatic there are a number of factors that you need to be aware of that can make the process more or less efficient.
A key pillar of the .NET framework is the automatic garbage collection which manages memory for all .NET applications. When the new operator is used to create an object, that object’s memory is taken from the managed heap. The garbage collector monitors unused (or ‘unavailable’ or ‘unreachable’) objects and when sufficient ‘garbage’ has accumulated it performs a collection to free up memory. This is an automatic process but to write efficient C# applications we must still have an appreciation of the garbage collector.
First it is important to understand the life-cycle of a managed object:
Managed Code Object’s Life Cycle
- Memory for an object is allocated from the managed heap when new is called.
The memory is allocated and then the object’s constructor is called.
- The object is used for a period of time.
- The object is destroyed when all its references are explicitly set to null or
it goes out of scope.
- The object’s memory is reclaimed some time later and is available for other objects to use.
The managed heap is essentially a block of contiguous memory. When a new object is created, that object’s memory is allocated at the next available location on the managed heap. If there is available memory the garbage collector does not need to search for space and allocations are very fast. If the memory is insufficient to create the object, the garbage collector attempts to reclaim space for the new object.
To reclaim memory the garbage collector collects and destroys objects which are no longer available which is when there are either no references to it, all the references are set to null, or else all references to it are from other objects that can be collected. The process of a collection involves moving available objects into the memory and reclaiming the memory used by objects which are no longer used. An object Which survives a collection is automatically promoted to the next generation (see below).
The garbage collector classifies all objects into on of three generations (Gens) uses three generations to group objects by their lifetime and
- Gen 0 – Newly created objects. Gen 0 is collected frequently to ensure that short-lived objects are quickly collection and memory released. Objects that survive a Gen 0 collection are promoted to Gen1.
- Gen 1 - Collected less frequently than Gen 0 and contains longer-lived objects which were promoted from Gen 0. Objects that survive a Gen 1 collection are promoted to Gen 2.
- Gen 2 – Objects promoted from Gen 1 (i.e. the longest-lived objects) and collected infrequently. The overall strategy of the garbage collector is to collect and move longer-lived objects less frequently.