Thursday 10 September 2009

What, Why and How Garbage Collector?

You can find lots of useful information from the msdn and other webpages...

Here are some points for understanding Garbage Collector (GC)

We can say Garbage Collector is used to free the memory (to allocate for another object) used by an object when the object is not longer used.

Considering the process of the GC, we can say:
1. When we create any object using new, the runtime will allocate memory in the next available location in the heap for that object to live.

2. It will be used by our code for sometime.....

3. The object is considered as not reachable object when the object is not referred by any other object, or the object is not referring any object object, or all the reference are set to null for that object or when the execution come out of the scope

4. The Step 1 to 3 will be continuing for the object whatever creating at runtime. So the heap will be blocked by the objects.

5. The Garbage Collector (GC) decides to recollect the memory from the object when there is not enough memory for the new object creating (as per point 1).

6. The GC will collect the objects which are not longer reachable (as per point 3). Then it will move the reachable object in to the contiguous space and reclaim the memory used by other object. So the memory will be freed from the unused/unreachable object.

The collection process is fully automatic, but there are ways we can do programming to make the GC process more efficiently.


for more information, pls refer the msdn site.


0 Responses to “What, Why and How Garbage Collector?”

Post a Comment