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:
- Objects that were recently allocated are most likely to be freed.
- Objects that have lived the longest are least likely to be become free.
- 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.
Finalizers
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() { base.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
~class1 { // Clean external resources. }
Declaring a Finalize method and destructor in a class, will lead to an error.
Dispose
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.. GC.SuppressFinalize(this); }
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.
Summary
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.
Backtrack: http://www.dotnetspider.com/resources/1149-Understanding-Garbage-Collection-C.aspx