Garbage Collection In .NET

In the .NET framework, each and every type identifies some resources which are available for the program. To use these resources, the memory needs to be allocated which represents the type. The following steps are required to access a resource. 
  1. Allocate the memory for the type that represents the resource (via new operator).
  2. Initialize the memory to set the initial state of the type and make the resource available (object construction).
  3. Use the resource.
  4. Tear down the resource.
  5. And free memory. This is where the garbage collector comes into the picture.

Garbage Collection Algorithm

 
When the application calls a new operator to create an object, there might not be enough space left in the managed heap to allocate the object. In case of insufficient space, CLR performs garbage collection, which works in the generations. CLR garbage collector is also known as Ephemeral Garbage Collector (Generation based GC). CLR GC makes the following assumptions about the code.
  1. The lifetime of the new object is short.
  2. The lifetime of an old object is likely to be longer.
  3. Collecting a portion of the memory (heap) is comparatively faster than collecting the whole heap.
.NET framework has 3 generations
 
Gen0, Gen1, and Gen2.
 
Gen0 is the basic layer in which GC would like to perform the memory cleaning and the objects which are on Gen1 and Gen2 are considered to be in memory for a long time and have survived for more than one GC.
 
Considering the above 
 
CLR uses a reference tracking algorithm. This algorithm cares only about the reference type variable because these are the only ones that can refer to the object on the heap, which is known as the Root Object. Value type contains the values directly. When CLR starts GC, it suspends all the threads in the process to prevent them from accessing the objects and changing their state while CLR examines them. CLR then performs the Marking phase – it walks through all the objects in the heap and looks for all the active roots to see which object they refer to. This way CLR tracks the reference. Any root referencing to an object on the heap makes the GC to mark that object. When an object is marked CLR examines the root inside and marks the object they refer to and if CLR finds out that the object is already marked it does not examine the object’s field again which prevents an infinite loop from occurring in the case where we have a circular reference.
 
Once marking is done, CLR knows which objects must survive and which object can be deleted, then the Compacting phase kicks in. In the compacting phase – CLR shifts the memory consumed by marked object down in the heap, compacting all the surviving objects together so that they are in continuation in the memory.
 
After the compacting phase is complete CLR resumes all the application’s threads and they continue to access objects as GC never happened at all.
 

Garbage Collection Triggers

 
CLR kicks-in GC when Gen0 has filled its budget, this is considered to be the most common GC trigger, along with this the following are also responsible as GC triggers,
  1. The code explicitly calls System.GC’s static Collect method.
  2. Windows is reporting low memory conditions
  3. The CLR is shutting down normally, (not via an unhandled exception)
  4. The CLR is unloading an App Domain

Garbage Collection Modes

 
When the CLR starts, it selects a GC mode and this mode cannot change during the lifetime of the process. There are two basic modes –
  1. Workstation
     
    Fine-tunes the GC for the client-side application and optimized to provide for low-latency GCs in order to minimize the time an application’s threads are suspended, so that it does not affect the end-user. GS assumes that the other applications are running on the machine and do not hold CPU resources.
     
  2. Server
     
    Fine-tune the GC for server-side application and optimized for throughput and resource utilization. In this mode, GC assumes no other applications are running on the machine and it assumes that all the CPUs on the machine are available to assist the GC
By default, the application runs on Workstation Mode. If the server application is running on the uniprocessor machine, then the CLR will always use Workstation mode GC. A Server application that hosts the CLR can request the CLR to load the Server GC.
 

Summary

 
This article is about some facts on Garbage Collection, the algorithm on which CLR’s GC work,s and its triggers and modes.
 
Happy learning !!


Similar Articles