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.
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
1.http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
2.http://www.c-sharpcorner.com/UploadFile/DipalChoksi/UnderstandingGarbageCollectioninNETFramework11292005051110AM/UnderstandingGarbageCollectioninNETFramework.aspx