Understanding Garbage Collection in C#

This article explains the garbage collection mechanism, provided in the .NET Framework, along with the relevant code snippets 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..

If we are creating and using objects that have Dispose or Close methods, we should call these methods when we've finished using these objects. It is advisable to place these calls in a finally clause, which guarantees that the objects are properly handled even if an exception is thrown.


The System.GC class provides methods that control the system garbage collector. We have to use methods from this class in our application with extreme caution.


Guest Author: Eirian19 Mar 2012

1st of all ,I like this piece and think more like it should be written by other leaders,and what I'm about to share is only my opinion .One of the things that caught my eye when I saw your status on Facebook was that you researched for your denomination about this topic,I think that is the primary concern that causes confusion in the Body of Christ,denominations presents groups,that form their own views ,that causes controversies,that brings about confrontation ,that creates division,that eliminates any opportunity of unification that could bring any resemblance of a body that's working cohesively to build hope for people in dire straights.As long as the the enemy causes these issues in the church ,there can never be real productivity to command change and restore hope ,that gives sinners a chance for salvation,NOTHING PERSONAL ,JUST MY OPINION

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: