Understanding Garbage Collection In C#

Garbage collection technique is automotive process. Means no one need to call garbage collection program. When it finds that there is need for memory, it runs itself and removes unused object from memory.So, unused objects are automatically released by automatic garbage collection in .NET C# by executing Garbage collector automatically when system has low physical memory.

Whats does Garbage means in C#?

"Garbage" consists of objects created during a program’s execution on the managed heap that are no longer accessible by the program. Their memory can be reclaimed and reused with no averse effects.

Advantage of Garbage Collector

No worry about memory management

Allocate object memory on managed heap efficiently.

Reclaims objects that no longer being used, clears their memory and keeps the memory for future allocations.

Provides memory safety by making sure that object cannot use the content of other object.

Let take a look at working Console application example of how we can call garbar collector explicitly in .NET C# but we before we proceed for that, generally we know that, we write destructor to destroy the object which is created by constructor, but in .NET destructor not runs automatically. Let us see how it works.

Now, if we want to free all unused object from memory, we call garbage collector explicitly using GC.collect() method. Here is GC is garbage collector class. In below example we are removing all unused object from memory using GC.collect() .

Note: Calling GC.Collect is discouraged by Microsoft and is generally to be avoided. The garbage collector is a very intelligent mechanism, and has much more information at its disposal than you do when it evaluates whether or not a collection is needed.

How Garbage Collector works

Garbage collector initialized by CLR and allocate memory for object, this memory is known as managed heap. Managed heap organized into form of Generation that contains reference of objects. Garbage Collector divides complete managed heap into three generation as:

How garbage collector identify that objects in use or not

Garbage collection handler - Handles that points to managed object and can be allocated by user code or by CLR.

Stack data - Static object in application domain that could be referencing other objects. Each application domain keep track of its static objects.

In Microsoft’s implementation of the .NET framework the garbage collector determines if an object is garbage by examining the reference type variables pointing to it. In the context of the garbage collector, reference type variables are known as “roots”. Examples of roots include:

A reference on the stack

A reference in a static variable

A reference in another object on the managed heap that is not eligible for garbage collection

When the method starts to execute, a List<object> is instantiated on the managed heap along with several objects. The List contains a root to each of the objects, and the stack contains a root to the List. While the method is executing, all of these roots are accessible from within the program and are considered to be “active”. When the method finishes executing, the stack is cleaned up, removing the root pointing to the List. The List is now no longer accessible within the program. All of the roots contained by the List (those pointing to the objects) are now considered to be "inactive".

The garbage collector identifies garbage by examining an application’s roots. Objects which have no active roots pointing to them are considered to be garbage.