Saturday, September 10, 2011

Garbage Collection

Que: What is Garbage collection?
Ans: Method of de-allocating and acquiring object unused objects.

Que: Is it possible to force Garbage collection?
Ans: Different people have different view on this, but there is method available GC.Collect(), which can help us to request Garbage collector to be invoked.

Que: What is the meaning of Generation?
Ans: Generation is term used by GC to identify how new an object is. object in generation 0 are newly created. Objects that survive the garbage collection once are in Generation 1 and objects that survive the garbage collection 2 or more are in generation 2.

Que: Explain the process of generational garbage collection.
Ans: Generational garbage collection makes following assumptions:
a) The newer an object is, the shorter is lifetime will be.
b) The older an object is, the longer its lifetime will be.
c)Collecting a portion of heap is faster than collecting the whole heap.
When CLR initializes, it selects a budget size for generation 0(say 256). Once this budget get full, it start garbage collection. Those object that survive are considered in generation 1(which also have some specific limit of memory). Once generation 1 budget get full CLR again call GC and collect the object in generation 1 and those which survived are considered in generation 2. CLR doesn't have more than generation 2. Objects which survived in generation 2 are still considered in generation 2 only. For optimization purpose CLR dynamically can change the budget of different generation.

Que:Explain the behavior of following program.
public static void main(){
Timer t = new Timer(TimerCallBack, null , 0 , 2000);
Console.ReadLine();
}
private static void TimerCallBack(object o)
{
    GC.Collect();
}
Ans: Ideally timer should be called every 2 sec, but as in call back GC.Collect is there thus through GC timer object will be collected as GC will see it is no longer used in main.

Que: What is drawback of finalization method?
Ans: IF we are using finalize method in our class then while creating an object of this class a separate pointer to this object is allocated(other than heap memory) on finalization list (a separate datastructure controlled by GC) and when this object is no longer in used GC will move this pointer from finalization to freachable queue. Next time when GC runs it will collect this memory. Hence we can understand it is taking more time to release memory.

Que: What is difference between Dispose and finalize?
Ans: Dispose method is used to clean up unmanaged resources and can be controlled by program while Finalize does the same thing but done by GC. We recommend to use IDispose interface and implemet Dispose method instead of using finalize method if we are using unmanaged resources.

Que: What is the meaning of resurrection?
Ans:In finalization object required and is dead, then GC forces the object back in life so that its finalize method can be called(freachable queue). After this finalize method is called and object is permanently dead. Act of preparing to call an object's finalize method is a form of resurrection.

No comments:

Post a Comment