.Net Core Garbage Collector

by | Sep 6, 2023 | .Net Core, Learn | 0 comments

Garbage collection


Garbage collection (GC) in .NET Core is an automatic memory management technique that reclaims unused memory. It simplifies memory management for developers and reduces memory leaks and bugs.


The GC identifies and collects objects that are no longer accessible to the application code. It scans the heap, where objects are stored, to determine which objects are still in use and which can be reclaimed.


The garbage collection process in .NET Core involves several steps:


Allocation: Memory is allocated on the managed heap when objects are created using the new keyword or similar methods.


Reachability: The GC maintains a set of root objects and traverses the object graph, marking reachable objects. Unmarked objects are considered garbage.


Marking: Reachable objects are marked as gray, while unvisited objects are marked as white. The marking process progresses until all reachable objects are marked as black.


Tracing: The GC traces object references, starting from the roots, to follow pointers and mark reachable objects. This continues until all reachable objects are marked.


Finalization: Objects with a finalizer are allowed to perform cleanup operations before memory reclamation. They are put on a finalization queue and processed later.


Reclamation: Memory occupied by marked garbage objects is reclaimed and made available for future allocations.


Compaction: The GC may perform compaction to reduce memory fragmentation. Surviving objects on the heap are rearranged, reducing fragmented free memory.


.NET Core uses a generational garbage collector that divides objects into different generations based on age. Younger objects are more likely to become garbage, while older objects tend to live longer. This generational approach optimizes garbage collection by focusing primarily on younger objects.


Garbage collection in .NET Core automates memory management, ensuring efficient memory usage and deallocation. It relieves developers from manual memory management tasks and promotes safer and more productive programming.


Submit a Comment

Your email address will not be published. Required fields are marked *

Related Posts

.Net Core vs .Net Framework differences

CLR vs Core CLR What is CoreCLR The CoreCLR provides many similar functionalities as the CLR, but it is specifically designed for the cross-platform nature of .NET Core. General CLR feature includes features like Just-In-Time (JIT) compilation, garbage collection,...

Understand Middleware in .Net Core

Middleware is a piece of software that handles http request and response. In simple term, .net core works with middleware in order to enhance performance of our application rather than having big chunk of framework component/ libraries at once, for which most of the...

Learn How .Net Core Hosting Works

Hosting In .NET CoreIn ASP.NET Core, there are two hosting models available: in-process hosting and out-of-process hosting.In-process Hosting (Default): In the in-process hosting model, a single web server (e.g., IIS) is used directly to host the application. To...