Tuesday 4 November 2014

How does the garbage collector work ?

What is garbage?

“Garbage” consists of objects created during a program’s execution on the managed heap that are no longer accessible by the program. Their memory can be reclaimed and reused with no averse effects.

What is the garbage collector?

The garbage collector is a mechanism which identifies garbage on the managed heap and makes its memory available for reuse. This eliminates the need for the programmer to manually delete objects which are no longer required for program execution. This reuse of memory helps reduce the amount of total memory that a program needs to run. In technical terms, we say that it keeps the program’s “working set” small.

Advantage of Garbage Collector

  1. Allow us to develop an application without having worry to free memory.
  2. Allocates memory for objects efficiently on the managed heap.
  3. Reclaims the memory for no longer used objects and keeps the free memory for future allocations.
  4. Provides memory safety by making sure that an object cannot use the content of another object.

How does the garbage collector identify garbage?

In Microsoft’s implementation of the .NET framework the garbage collector determines if an object is garbage by examining the reference type variables pointing to it. In the context of the garbage collector, reference type variables are known as “roots”. Examples of roots include:
  • A reference on the stack
  • A reference in a static variable
  • A reference in another object on the managed heap that is not eligible for garbage collection
  • A reference in the form of a local variable in a method
Take the following method.
void CreateList()
{
 var myList = new List<object>();
 myList.Add(new object());
 myList.Add(new object());
 myList.Add(new object());
 Console.WriteLine("Objects added!");
}
When the method starts to execute, a List<object> is instantiated on the managed heap along with several objects. The List contains a root to each of the objects, and the stack contains a root to the List. While the method is executing, all of these roots are accessible from within the program and are considered to be “active”. When the method finishes executing, the stack is cleaned up, removing the root pointing to the List. The List is now no longer accessible within the program. All of the roots contained by the List (those pointing to the objects) are now considered to be “inactive”.
The garbage collector identifies garbage by examining an application’s roots. Objects which have no active roots pointing to them are considered to be garbage.

Generations in Managed Heap

The managed heap is organized into three generations so that it can handle short lived and long lived objects efficiently. Garbage collector first reclaim the short lived objects that occupy a small part of the heap.
  1. Generation 0

    This is the youngest generation and contains the newly created objects. Generation 0 has short-lived objects and collected frequently. The objects that survive the Generation 0 are promoted to Generation 1.
    Example : A temporary object.
  2. Generation 1

    This generation contains the longer lived objects that are promoted from generation 0. The objects that survive the Generation 1 are promoted to Generation 2. Basically this generation serves as a buffer between short-lived objects and longest-lived objects.
  3. Generation 2

    This generation contains the longest lived objects that are promoted from generation 1 and collected infrequently.
    Example : An object at application level that contains static data which is available for the duration of the process.

No comments:

Post a Comment