Thursday, December 29, 2011

How Does the Garbage Collector Work in .NET

While you are doing application programming and when you are creating objects of classes, have you ever wondered, in the end what will happen to all these objects you have created. Well I have always wondered and today I am going to write about this nice mechanism called Garbage Collection which is responsible for proper resource management in .NET.

Every object that you create has a life cycle, from creation to destruction. When an object is destroyed, its state must be cleaned, and any managed resources used must be reclaimed. And that's where the Garbage Collector comes in. Garbage collection in the Microsoft .NET common language runtime environment, completely absolves the developer from tracking memory usage and knowing when to free memory.

Implementing a proper resource management mechanism by us for our own applications would be a difficult and time taking task. It will surely distract our concentration on the real problems that we are going to solve. Garbage Collector will take all the trouble of doing the resource management, so we will only have to focus our minds on our real problem.

So as I have mentioned before, every object that we create has a life cycle.
  1. First, block of memory will be allocated and that block of memory is big enough to hold the object.
  2. Then by calling the constructor we initialize the object. Now memory will be converted into an object.
  3. Object can be then used by our application.
  4. Object will then arranged for clean up.
  5. After the cleaning up is completed, the allocated memory for that particular object is reclaimed.
From above things, we can only control the second step and the fourth step. We call the constructor for the second step and we can implement and call the destructor for the fourth step.

So How Does the Garbage Collector Work

The garbage collector runs in its own thread and normally runs automatically, under well defined circumstances. When the garbage collector runs, other threads in an application are halted because the garbage collector may move objects in memory and must update pointers to the correct addresses for these objects.

The steps that the garbage collector would take to reclaim resources are as follows.
  1. It marks every object as dead; objects are considered dead unless proved otherwise.
  2. It starts from objects referenced on the stack, marking referenced objects as alive. It performs this recursively; if an object that is already marked as alive references another object, that object is also marked as alive. The garbage collector includes logic to prevent infinite recursion, for example, where there is a circular reference between two objects.
  3. It checks whether any of the objects that have been marked as dead have a destructor that must be run. Running the destructor is referred to as finalization. Any objects that require finalization are moved to a data structure maintained by the garbage collector called the freachable queue. The freachable queue stores pointers to objects that require finalization before their resources can be reclaimed.
  4. Objects added to the freachable queue are marked as alive because there is now a valid reference to them; the destructor must be run before their memory can be reclaimed. Objects are normally added to the freachable queue only once.
  5. Objects marked as alive are moved down the heap to form a contiguous block, defragmenting the heap. References to objects (on the stack and in other objects on the heap) moved by the garbage collector are updated.
  6. Other threads resume.
  7. On a separate thread, objects added to the freachable queue are finalized. After an object is finalized, the pointer to that object is removed from the freachable queue. Objects are not removed from memory until the next time the garbage collector runs.
So that's how the garbage collector works in .NET framework. Please feel free to give me your valuable feedback.

Happy Coding.

Regards,
Jaliya