Introduction to Garbage Collection in .NET
 
 
 
Author : Thomas Kurian Ambattu
 
A word for beginners: - This article is for beginners who want to know the mechanism of Garbage Collection in. NET. I suggest that people who read this have a basic understanding of C & C++ .I’m trying to explain the mechanism of GC without the influence of C & C++ but as I explain some points I can’t avoid at least some of the basics of C & C++ like pointers, memory allocation in heap etc. Any way I’ll try my best to explain this mechanism in the simplest way so that it will be easy for a beginner to understand.

One of the important advantage perhaps I would say the most attractive feature of .NET is the concept of Garbage Collection. Now the question is what is this Garbage Collection? Garbage Collection is an automatic memory management mechanism in .NET. This mechanism helps to free the memory automatically when they are no longer used by the application. The advantage is that developers need not worry about the memory management when writing the code i.e. the GC will do it for you as and when needed. I think C & C++ programmers will understand the above said point better than anybody else.

A little about C, C++: - In C & C++ if we allocate the memory using new operator we have to free it by ourselves. There is no mechanism that will automatically free the memory that is allocated. So people often make two common mistakes, they either forgets to free the memory allocated or try to use the memory they already freed. The consequences of these two bugs will be very severe and the results will be unpredictable. This is called a resource leaks or memory leaks in application which will be a headache to programmers.

Again back to our topic, The Common Language Runtime of .NET requires all the resources to be allocated from the managed heap. When a program is loaded into the memory the runtime reserves a region of address space for that application. This reserved memory space is called Managed Heap. This space initially will have no storage allocated. This space is only used when an object of this particular program is loaded into the memory. So now the question is, how is the object inserted in the heap? The answer is not that easy, In order to indicate where the object is to be inserted the heap always maintains a pointer. The heap maintains this pointer in the position just below the last object inserted. Initially this pointer is set to the base address of the reserved space region.

Managed Heap

Now let us see how the object is created and allocated in the heap. An application creates an object with the new operator. The operator checks for whether the space required by the objects meets the reserved space. If it meets the space then the object is inserted at the address location where the pointer is points. Now the pointer changes the location where it points. The pointer is shifted to the location just below the newly inserted object. The figure below shows the details of this

 
The Garbage Collection Process

Consider a situation where an application creates an object and there is not enough space in the heap to allocate for this situation. The heap has a mechanism by which it checks whether there is enough space for the object to be allocated. The heap checks this by adding the size of the object to our NextObjPointer. If the NextObjPointer is beyond the reserved address space region, then the heap is full and so it should make a collection to free some memory. So the GC now travels through the heap and find outs the objects that are no longer used by the application and those dead objects are cleared to free the memory and the free space is created in the heap. Although I said the process in a simple way the real situation is not that easy it’s a little complicated. Actually a collection occurs when the Generation 0 is full. The Generation is a mechanism by the Garbage Collector to improve the performance, another thing is the garbage collection is performed not in a simple manner as what I said above, it is actually done on the basis of an algorithm.

 
Hope you all got an idea about the Garbage Collection in .NET.
I’ll discuss about generations & Garbage Collection Algorithm in my next articles Garbage Collection Internals I II & III. Do mail me your feedback on this article to feedback@netkidoos.com. All suggestions & views will be appreciated.
 
 
 
 
 
 
     
     
     
   
 Copyright © 2003 NETKidoos.com All rights reserved Terms Of Use
Best viewed in 1024 X 768, IE 5.0 and above