Understanding Garbage Collection in Microsoft.NET


Garbage collection in Microsoft.NET is based on Mark and Compact algorithm and concept of generations.

 

Mark and Compact Algorithm

 

When an application is started a given amount of address space is allocated to the application which is called managed heap. We can make use of GetTotalMemory method of GC class to get the number of bytes currently allocated in managed memory. When a new object has to be instantiated and there isn’t enough space in the application’s managed heap, the garbage collection process runs.

 

  • Garbage collector generates a list of all the application’s objects that are still in scope.

 

  • The garbage collector steps through the managed heap, looking for any object that is not present in this list.

 

  • The garbage collector marks the objects that are out of scope. The space these objects were taking up is freed.

 

  • After the garbage collector has iterated through the managed heap, it moves the references to all the objects down to the beginning of the heap. All the object references are therefore stored contiguously in the heap.

 

 

Let’s take a simple example

 

Suppose when an application is started, objects A, B, C and D are instantiated, and references to these objects are placed on the managed heap.

 

Object D

Object C

Object B

Object A

 

Suppose later in the life of application, object E needs to be instantiated. Now there isn’t enough space in the managed heap so garbage collection needs to be performed. Objects B and C have gone out of scope.

 

When garbage collection is started, a list of all the objects that are still in scope is generated, here the list will contain object A and D, than it iterates through the managed heap, looking for the objects that are not present in the list. These objects are marked as unreachable and the managed heap is compacted. A reference to Object E is then placed on the heap.

 

 

Object D

Object C

Object B

Object A

 

To

 

 

Object E

Object D

Object A

 

 

 

Generations

 

Here in the above case it could be very expensive to step through the entire heap, looking for marked objects. To make this process more efficient, Microsoft.NET implements concept of generations. The theory of generations assumes that the older an object is, the less likely it is out to be out of scope during garbage collection. Therefore, it is most efficient to check the newest objects first and then check the older objects if space is still needed.

 

There are three generations: Generation 0, 1 and 2. When a garbage collection is performed, it collects only objects in Generation 0. If there is still not enough space to allocate a new object, then garbage collector moves on to Generation 1, and then if necessary, it moves to Generation 2 objects.

For e.g. consider an application that has object A in generation 2, B and C in generation 1 and D, E, F in Generation 0.

 

 

Object F

Gen 0

Object E

Gen 0

Object D

Gen 0

Object C

Gen 1

Object B

Gen 1

Object A

Gen 2

 

Now the application attempts to allocate space to Object G, but it cannot, so a garbage collection is required.

 

Say objects C, D and E have gone out of scope.

 

After the garbage collection, objects D and E are collected. Even though Object C had gone out of scope, the garbage collector only collected from Generation 0. If more space had been required than Object C, from Generation 1, would also have been collected.

 

And one more thing, the Object F will be moved to Generation 1 because it has survived one garbage collection. The garbage collector did not collect from Generation 1; therefore Object C is still within generation 1.

 

Now the managed heap would look something like this

 

 

 

Object G

Gen 0

Object F

Gen 1

Object C

Gen 1

Object B

Gen 1

Object A

Gen 2

  

 

Bye ..

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

7 thoughts on “Understanding Garbage Collection in Microsoft.NET”

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM