Garbage collector

Garbage collector

Just like a normal Garbage collector in our real life helps to remove unwanted materials, in the same way the Garbage collector in dot net allocates and releases the memory from our application.
Whenever we create a new object, the common language runtime which in short known as CLR allocates memory for the object. Till the time address is available in the managed heap, CLR continues to allocate the memory for the newly created objects. Garbage collector keeps on checking if any object does not require memory or in no use, then it performs some operations required to make the unused space free.


Author: satyajee srivastava21 Dec 2010 Member Level: Silver   Points : 1

The garbage collector checks to see if there are any objects in the heap that are no longer being used by the application. If such objects exist, then the memory used by these objects can be reclaimed. (If no more memory is available for the heap, then the new operator throws an OutOfMemoryException.)
The garbage collector offers an additional feature that you may want to take advantage of: finalization. Finalization allows a resource to gracefully clean up after itself when it is being collected. By using finalization, a resource representing a file or network connection is able to clean itself up properly when the garbage collector decides to free the resource's memory.
Thus The garbage collector is optimized to perform the memory free-up at the best time based upon the allocations being made.

Visit these links for GC in .NET

Author: Awaneesh Jatrana21 Dec 2010 Member Level: Bronze   Points : 0

wow dts gr8

Author: murali03 Jan 2011 Member Level: Bronze   Points : 1

The applications created acquire memory. Memory management includes deallocating this acquired resources and acquiring them. This is done by garbage collector and this concept of automatically reclaiming the memory is called Garbage Collection.

Author: Jayendra Kumar06 Jan 2011 Member Level: Gold   Points : 0

This is really good article about garbage collector.

Author: sugandha16 Jan 2011 Member Level: Gold   Points : 1

Garbage collection is a CLR feature which automatically manages memory. Programmers forget to release the objects while coding ..... laziness.CLR automatically releases objects when they are no longer referenced and in use.CLR runs on non-deterministic to see the unused objects and cleans them. One side effect of this non-deterministic feature is that we cannot assume an object is destroyed when it goes out of the scope of a function. Therefore, we should not put code into a class destructor to release resources.

GC uses 2 methods for garbage collection:
1. Mark ->In this object that are not reachable are marked for collection. While applications running with the new keyword, an object is created on the heap.

2. Sweep/Contact -> In this non- garbage objects are compacted.The objects are allocated on heap contiguously. The compaction of object is performed by moving all live objects at teh bottom of heap.. This makes free space at the top of the heap.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: