Question

I have a confusion regarding these two seemingly contradictory statements on msdn..

1) When a class contains a destructor, an entry is created in the Finalize queue. When the destructor is called, the garbage collector is invoked to process the queue.

2) The programmer has no control over when the destructor is called because this is determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. If it considers an object eligible for destruction, it calls the destructor (if any) and reclaims the memory used to store the object.

In the first statement i understand that Destructor calls ---> Garbage collector while as from the second statement i understand that Garbage collector calls ---> Destructor

??

Update: this link

Was it helpful?

Solution

First the garbage collector determines that an object is eligible for collection.

If the object requires finalization then it is placed on the finalization queue; it is now alive again because the queue is a root.

Then the finalizer thread runs, marks the object as no longer requiring finalization, and runs the finalizer.

If at the end of the finalizer the object is still dead then when the GC runs again later, it discovers that there is a dead object that does not require finalization, and cleans it up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top