Managed Unmanaged code Garbage Collector Generations

What is the difference between Managed code and Unmanaged code Garbage Collector and Generations in C#?

In this article you will understand What is the difference between Managed code and Unmanaged code Garbage Collector and Generations in C#

Interview Questions Part 1 – https://sagarjaybhay.com/interview-questions-for-experienced-beginner-net-professionals-series-part-1/

Managed Code:

The code which runs under the control of CLR is called a managed code. The code which is written in the .NET framework is managed code.

Managed code uses CLR which looks after your applications by managing your application memory handling security and allow to call other application from managed code.

.Net framework ultimately called a managed code. For Managed code, CLR provides different services likes garbage collection, exception handling like that.

UnManaged Code:

The code which runs under its own runtime is called unmanaged code. It means the code which not runs under the control of CLR is called unmanaged code.

For example, an application written in C or C++ or java is called as unmanaged code on that CLR doesn’t have any control.

What is the Garbage collector and what his generation and different generation?

In CLR the GC garbage collector is present which serves the purpose for automatic memory management. It is a feature provided by CLR to free managed objects.

Automatic memory management is made possible by Garbage collection in .Net Framework. When we create a class object in our program the memory gets allocated on the heap.

When we no longer use that object garbage collector collects that object and free that memory.

Advantages garbage collector

  1. No need to manually free the memory
  2. It allocates the object on the managed heap
  3. If Gc found that the object is no longer use then it will clear that memory and keep this memory available for future allocations.

When garbage collection occurs

Below is the condition when that satisfied GC will run.

  1. If a system has low physical memory then garbage collection is necessary.
  2. When the pre-set threshold is exceeded then garbage collection will occur.
  3. When you call GC. Collect method then garbage collection will occur.

class Program
    
        static void Main(string[] args)
        
            demo d = new demo();
            Console.WriteLine("Number of Generation Supporting "+ GC.MaxGeneration.ToString());
            Console.WriteLine("Generation for this object  " + GC.GetGeneration(d));
            Console.WriteLine("Generation Total Memory  " + GC.GetTotalMemory(true));
            Console.Read();
        
    


    class demo
    
        private int i = 0;
        private static string str = string.Empty;

        public void calculate()
        
            for (int i = 0; i < 100; i++)
            
                str += i * 1000;
            
        
    

In a background, GC thread is running continuously and it checks the generations whether or not an object is unused or not to reclaims its memory. It only cleans managed objects, outside CLR boundary it will not reclaim their memory.

Generation 0, Generation 1, Generation 2.

Why we need Generations?

Suppose we have a big application in that we are creating 1000 objects. So every time to check 1000 objects it’s cumbersome so what it did he creates the generation and which is generation 0, generation 1 and generation 2 and when first-time objects are created they are put in generation 0 bucket.

When second-time garbage collector thread runs then it will check which objects are required and which are not. If he finds 400 not required and 600 are actively required then these 600 objects he moves in next bucket which is generation 1 and then again when he check he find 200 required only and rest are not then 200 moves in next bucket and remaining are removed and garbage collector less likely to check the generation 2 bucket so performance automatically increases.

  public partial class Form1 : Form
    
        public Form1()
        
            InitializeComponent();
        

        private void button1_Click(object sender, EventArgs e)
        
            for (int i = 0; i < 1000; i++)
            
                XYZ x = new XYZ();

            
        
    

    class XYZ
    
        public int i  get; set; 
        public string str  get; set; 

        public void append()
        
            for (int i = 0; i < 1000; i++)
            
                str += i;
            
        

    

Garbage Collector Generations
Garbage Collector Generations

In the above image, we are able to see gen0 in that our object is available.

For this, we need to download the CLR profiler and from that CLR profile, you need to open your project EXE  and then click on Objects by address button you can see the above image by doing this.

CLR Profiler Images
CLR Profiler Images


GitHub Projects Link : – https://github.com/SagarJaybhay/DotNetDemos


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s