"Finalization Queue in .Net Garbage Collection"
In this article we will see the detailed working of the Garbage Collector and the Finalization Queue.
For reading about the Garbage Collector please go to the following link:
Garbage Collection in .Net
Getting Started:
When we create an object the memory is allocated to it in the Managed Heap but if the object has a Finalize() method then the pointer pointing to it is pushed into the Finalization Queue.So we can say that each pointer in the Finalization Queue points to the object that has a Finalize mathod call before the memory allocated to it is released.
For reading about the Finalize() method please go to the following link:
Finalize() method in .Net
Let us understand it more clearly with the help of a following example:
Suppose we have ten objects from 1 to 10, the objects 2,3,5,6,and 10 have their Finalize() method call therefore the pointers pointing to these objects will be pushed into the Finalization Queue. The following diagram will give a more clear idea:
Note:->Finalization Queue is nothing but a internal data structure that is managed by the Garbage Collector.
From the above diagram we can see that we have four root objects i.e. 1,3,4 and 6, the objects 3 & 6 further refers to the objects 8 & 10 respectively.
Now when the Garbage Collector start it starts from the Root and make a graph of all the objects that are reachable from the root. In the above diagram we can see that 1,3,4 and 6 are root objects and object 8 and 10 are reachable from 3 and 6 respectively therefore all these objects that are reachable from the root are considered to be the live objects and the objects 2,5,7 and 9 will be considered as dead objects.
Now as 2,5,7 and 9 are dead objects therefore they should be collected i.e the memory allocated to them should be released and reclaimed. But before collecting these dead objects the Garbage Collector looks into the Finalization Queue for these dead objects, if the pointer pointing to these dead objects is found in the Finalization Queue then those pointers are flushed from the Finalization Queue and are pushed into the Freachable queue. (Freachable queue is also the same as Finalization Queue, it is also a internal data structure managed by the Garbage Collector.)So we can say that each pointer inside the Freachable queue identifies those objects which are waiting for their Finalize() method to be called.
From the above diagram we can see that the objects 2,5,7 and 9 are not reachable and the pointer pointing to the object 2 and 5 are also in the Finalization Queue therefore these pointers will be flushed out from the Finalization Queue and will be pushed into the Freachable queue.
The following diagram will make it more clear:-
From the above diagram we can see that the memory of objects 7 and 9 has been reclaimed while the memory for the objects 2 and 5 does not, because as the pointer of 2 and 5 object move from Finalization Queue to Freachable Queue they are not considered to be the Dead Objects, instead a special run-time thread is invoked. The work of this thread is to remove each entry from the Freachable Queue by calling Finalize() method of each entry in the Freachable Queue .
Now when the Garbage Collector starts for the next time the memory of these objects are reclaimed as their Finalize() method has been executed and the Freachable Queue no longer points to it.
Hope it Helps.... Happy Coding..!
0 Comment(s)