When a dot-net application runs, it generates a large number of objects. It’s likely that some of those objects aren’t used by the programme at any one time. A garbage collector is a Small Routine or a Background Process Thread in the.NET Framework that runs periodically and tries to identify which objects are not currently being used by the application and deallocates the memory of those objects.
So, Garbage Collector is nothing more than a CLR functionality that allows us to clean up or delete unneeded managed objects. By cleaning or eliminating those useless managed objects, the memory is effectively reclaimed.
In garbage collections, Generations are just the factors that determine how long objects remain in the memory.
0 Generation (Zero): This generation contains objects that have a short lifespan, such as Temporary objects. In this generation, the trash collection operation is started regularly by the GC.
1 Generation (One): This generation serves as a buffer between items with short lives and those with extended lives.
2 Generation (Two): This generation stores long-lived objects, such as a static and global variables, that must be kept for a period of time. Things that are not collected in generation Zero are moved to generation 1 and are known as survivors; similarly, objects that are not collected in generation One are moved to generation 2 and remain in the same generation from there on.
The following are some of the GC class’s methods:
GC.GetGeneration(): This method returns the target object’s generation number. It just takes one parameter, which is the target object for which the generation number is needed.
using System;
public class Demo {
public static void Main(string[] args)
{
Demo obj = new Demo();
Console.WriteLine("The generation number of object is: " + GC.GetGeneration(obj));
}
}
Output:
The generation number of object is: 0
GC.GetTotalMemory() The number of bytes allocated in the system is returned by this procedure. It only requires a single boolean parameter, true indicating that the procedure waits for garbage collection to complete before returning, and false indicating the reverse.
using System;
public class Demo {
public static void Main(string[] args) {
Console.WriteLine("Total Memory:" + GC.GetTotalMemory(false));
Demo obj = new Demo();
Console.WriteLine("The generation number of object is: "
+ GC.GetGeneration(obj));
Console.WriteLine("Total Memory:" + GC.GetTotalMemory(false));
}
}
Output:
Total Memory:4197120
The generation number of object is: 0
Total Memory:4204024
GC.Collect(): This method can be used to force garbage collection in the system. Only one parameter is required for this method: the number of the oldest generation for which garbage collection is performed.
using System;
public class Demo {
public static void Main(string[] args)
{
GC.Collect(0);
Console.WriteLine("Garbage Collection in Generation 0 is: "
+ GC.CollectionCount(0));
}
}
Output:
Garbage Collection in Generation 0 is: 1
People having good knowledge of Financial accounting can get an accounting certification from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial Accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.