1. Back To Blog

What is Garbage Collector(GC) in C#?

Before we get into Garbage Collector concept and how does it works first we need to understand about Memory De-allocation.

We have two types of memory in .NET Core Framework C# applications one is stack and another one is heap where memory allocation is done simply and easy means when variables are declared memory allocation is done automatically according to the declarations.

  • The process of cleaning memory after variables goes out of scope then that process is known as memory de-allocation.
  • Generally, stack is contiguous in nature so it can allocate and deallocate  the memory for variables automatically by itself.
  • It is necessary to given back the allocated memory to the Operating System if system use the memory even after variables goes out of scope then it creates bad situations.
  • In heap memory deallocation is complex and can't happen automatically by it self because it is not contiguous in nature so for deallocation heap memory use the garbage collector.

From above its is clear that what is memory deallocation now we discuss about what is garbage collector.

Garbage collector is features of CLR (Common Language Runtime)and it's background running process which help us to clean unused managed objects.

Basically it reclaims the memory. In .NET framework while running C# application it will creates multiple objects and after certain time our garbage collector can checks for is their any unused managed objects if they found any kind of that objects then GC  cleans those objects and reclaims the memory. Garbage Collector is often called as generational approach because memory is divided into spaces called generations. It has three spaces GC0, GC1, GC2

Now here we discuss what is GC0, GC1, GC2 :-

  1. GC0: generation 0 hold the new created object that has never moved for collection. Normally all the temporary or sort life span objects are stored in heap storage of generation 0. In generation 0 there are much more amount of newly allocated of objects. So most of the time garbage collection is only used in generation 0.
  2. GC1: This generation identifies an object that has marked for collection but not removed. Because there was sufficient space in heap. If particular generation 0 items are not released during garbage collection process then they are transferred to generation 1. It is intermediate space between short-lived and long-lived layers.
  3. GC2: This generation holds the longest lived objects in the application. If things in generation 1 are not released they will migrated or promoted to generation 2 where those objects are being published. The reason behind this is that objects will exists primarily in heap memory for long time.

Important thing is that our garbage collector cab visit on those generation and checks for is this object still needed if not then that one is deleted by our garbage collector and reclaims the memory. Here we need to know clearly that generation 2 will have more memory than that of generation 1 and generation 1 will have more memory then that of generation 0.

Let's take a simple example :-




Here in the above program we are using different methods of garbage collector. Let us describe them :-
  • Method GC.GetGeneration():this function returns the target object's generation number.
  • Method GC.GetTotalMemory: The number of bytes assigned in the scheme is returned by this method.

By using these methods we also know in which generation that our object should stored.

Let us take an another example for knowing is our garbage collector working by using performance profiler.


Here we write simple program that can store the values in heap and now see is there our GC work or not so for that first we go on debug the we choose performance profiler the select memory status then the result will be as below :-


On the top of memory usage their are certain red lines of garbage collector that means our garbage collector is working over in that program.

From above example we got clear idea about what is garbage collector and what are generations in garbage collectors and so on. if you get any errors or anything in this article please comment down and we will help you in resolving the same.

Do watch the latest C# Interview Questions & Answers video covering very important concepts :-


Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social