By Shruthi T on Oct 14, 2016 5:39:36 AM
Whenever you instantiate an object in your .net application, some memory is allocated to store that object. But at some point that object may no longer be needed by your application. If you want to reuse that memory in your application, we have to de-allocate that memory first. And this process of freeing up or de-allocating memory which is no longer needed by the application is called Garbage collection.
Garbage collection is an automatic process, when object is created then it will be placed in the Generation 0. The garbage collection uses an algorithm which checks the objects in the generation, the objects life time get over then it will be removed from the memory. The two kinds of objects. One is Live Objects and Dead Objects. The Garbage collection algorithm collects all unused objects that are dead objects in the generation. If the live objects running for long time then based on that life time it will be moved to next generation.
The object cleaning in the generation will not take place exactly after the life time over of the particular objects. It takes own time to implement the sweeping algorithm to free the spaces to the process.
When a garbage collection starts, it looks at a set of references called the ‘roots’. These are memory locations that are designated to be always reachable for some reason, and which contain references to objects created by the program. It marks these objects as ‘live’ and then looks at any objects that they reference; it marks these as being ‘live’ too. It continues in this manner, iterating through all of the objects it knows are ‘live’. It marks anything that they reference as also being used until it can find no further objects. Once all of these live objects are known, any remaining objects can be discarded and those spaces can be re-used for new objects and it squashes the memory so that the free memory is always located at the end of a heap so that allocation of new objects will be faster.
How Often Does the Garbage Collector Perform a Garbage Collection?
There are three ways a garbage collection can be triggered.
Firstly, if your system has low physical memory, this can trigger a garbage collection.
Secondly, a threshold is defined which indicates an acceptable level of memory on the heap which can be used by allocated objects. If this threshold is surpassed, then a garbage collection is triggered.
Garbage collection can also be triggered by calling the GC.Collect method.
C# program that uses GC.Collect
public class Work
public int salary;
public int months;
public int CalculateApproxCTC()
salary = 1025454564;
months = 12;
int CTC = salary * months;
static void Main()
long mem1 = GC.GetTotalMemory(false);
// Allocate an array and make it unreachable.
int allocatespace = new int;
int values = new int; // Generation 2 Example
// just create an object of class and do not use - will be cleared by Garbage Collection
Work wrk = new Work();
Console.WriteLine(GC.GetGeneration(wrk)); // Generation 0 example
values = null;
long mem2 = GC.GetTotalMemory(false);
// Forces an immediate garbage collection of all generations.
long mem3 = GC.GetTotalMemory(false);