As we all know "Every program needs a memory and unfortunately which
is finite in terms of its availability".....:)

Software must cope with memory usage, and there are two ways to manage it.

  • Manually
  • Automatically

Manual management are more prone to errors especially with exceptions and in asynchronous code. This is why modern managed environments (.NET, Java, Erlang, and many more) implement automatic memory management with garbage collection.

Lets see what is Garbage Collection in C# world.

When a C# program instantiates a class, it creates an object.

The program manipulates the object, and at some point the object may no longer be needed. When the object is no longer accessible to the program and becomes a candidate for garbage collection.

There are two places in memory where the CLR stores items while your code executes.

  • Stack : stack keeps track of what’s executing in your code (like your local variables)
  • Heap : heap keeps track of your objects.

For an object on the heap, there is always a reference on the stack that points to it.

The garbage collector starts cleaning up only when there is not enough room on the heap to construct a new object.
The stack is automatically cleared at the end of a method. The CLR takes care of this and you don’t have to worry about it.

The heap is managed by the garbage collector.

In unmanaged environments without a garbage collector, you have to keep track of which objects were allocated on the heap and you need to free them explicitly. In the .NET Framework, this is done automatically by the garbage collector.

How the Garbage Collector works?

Let’s look at below diagram which i referred from microsoft site to understand it better.

enter image description here

Before Garbage Collector Runs:

In the above diagram, Before Garbage collector runs, the application root has dependency on object 1, object 3 and object 5.
Object 1 is dependent on object 2 and Object 5 is dependent on object 6. So the application root does not have any dependency on object 4 and object7.

When Garbage collector runs:

  1. Marking Phase:
    It marks all the heap memory as not in use then examines all the programs reference variables, parameters that has object reference and other items that point to heap objects

  2. Relocating Phase:
    For each references, the garbage collector marks the object to which the reference points as in use

  3. Compact Phase:
    Then it compacts heap memory that is still in use and updates program reference
    Garbage collector updates the heap itself so that the program can allocate memory from unused portion

After the Garbage collector runs:

It discards Object 4 and Object 7 since there is no dependency exists
and compact the heap memory. When it destroys an object, the garbage
collector frees the object’s memory and any unmanaged resource it

Forcing Garbage Collection:

You can force this by adding a call to GC.Collect but it is always recommended not to call garbage collector explicitly.

Hope it helps!