Garbage Collection in .NET

Garbage collection in the .NET Framework allows automatic memory management. When a class object is created at runtime, heap memory is allocated to it.

Generation 0

Generation 0 of the heap memory contains all the short-lived objects, such as temporary variables and all newly allocated objects. In general, generation 0 has the highest frequency of garbage collection.

Generation 1

This generation contains longer-lived objects that were promoted from generation 0. Objects that make it through generation 1 are promoted to generation 2. Essentially, this generation acts as a buffer between short-lived and long-lived objects.

Generation 2

If some generation 1 objects do not get released in the next garbage collection run, they are moved to generation 2. Objects in generation 2 are long-lived and are similar to static objects because they remain in heap memory throughout the process.

Three phases

Garbage collection then proceeds through three phases: • A marking phase that creates a list of live objects. • A relocating phase that updates references to the list of live objects. • A compaction phase that releases dead objects and compacts and moves live objects.

2X faster app development !

Syncfusion offers over 1,800 components and frameworks for WinForms, WPF, ASP.NET (MVC, Core), UWP, WinUI, .NET MAUI, Xamarin, Flutter, Blazor, JavaScript, Angular, Vue, and React that make developers’ work easier.