Search content within the blog

Sunday, April 11, 2010

Understanding Garbage Collection in C#


All the garbage collection mechanisms have one thing in common, that is they take the responsibility of tracking memory usage.

Understanding Garbage Collection

The .NET garbage collector is optimized for the following assumptions

1. Objects that were recently allocated are most likely to be freed.
2. Objects that have lived the longest are least likely to be become free.
3. Objects allocated together are often used together.

The .NET garbage collector is known as generational garbage collector. The objects allocated are categorized into three generations. Most recently allocated objects are placed in generation 0.
Objects in generation 0, that survive a garbage collection pass are moved to generation 1.
generation 2 contains long-lived objects, that survive after the two collection passes.

A garbage collection pass for generation 0 is the most common type of collection. Generation 1 collection pass is performed if generation 0 collection pass is not sufficient to reclaim memory.
Atlast, generation 2 collection pass is peformed if collection pass on generation 0 and 1 are not sufficient to reclaim memory. If no memory is available, after all the collection passes, an
OutOfMemoryException is thrown.


A class could expose a finalizer, which executes when the object is destroyed. In C#, the finalizer is a protected method as shown below.
protected void Finalize()
// clean external resources
The method Finalize(), is only called by the .NET framework.

C#, will generate a code to a well formed Finalizer, if we declare a destructor as shown

// Clean external resources.

Declaring a Finalize method and destructor in a class, will lead to an error.


Instead of declaring a Finalizer, exposing a Dispose method is considered as good.

If we clean up a object, using Dispose or Close method, we should indicate to the runtime that the object is no longer needed finalization, by calling GC.SuppressFinalize() as shown below:

public void Dispose()
// all clean up source code here..