Sunday, January 31, 2010

.NET garbage collection

What is garbage collection?

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.

Is it possible to force garbage collection to run?     

Yes, it is possible to force Garbage Colletcion . The way to do it is by using GC.Collect().However, it is also necessary to run the finalizers of the objects that need to be deallocated. So, it is necessary to use GC.WaitForPendingFinalizers() along with GC.Collect() so that the finalizers threads are not executed separately. 

Define Dispose().

It is a method for releasing resources that an object acquires. The Dispose method is called by the destructor.

Explain how garbage collection manages reclamation of unused memory in .NET.

CLR performs garbage collection on small objects and large objects separately. It maintains separate heaps for these two types of objects. Large Objects are maintained in LO
Heap and small objects are kept in multiple heaps which are compacted regularly.
It uses the JIT compiler which provides it the references of live objects indicating that they are alive. The rest of the objects are then subject to garbage collection.
Then the free memory is merged and the occupied memory is compacted so that the live objects are contiguous.

Explain how garbage collection deals with circular references.

Circular referencing issue happens when two objects refer to each other. Usually in a parent-child relationship, situations occur where a child interacts with the parent object and has a reference held to the parent object.
The .NET, the objects that are reachable from the root can be cleaned up easily. Thus, this can even be applied to circular reference and have the objects holding the resources cleaned up.

0 Comments: