user1399653
user1399653

Reputation: 193

Garbage collector - finalization

From MSDN: "When an object is not reachable, the garbage collector considers the object garbage. Then, when the garbage collector moves an object's entry from the finalization queue to the freachable queue, the object is no longer considered garbage and its memory is not reclaimed. At this point, the garbage collector has finished identifying garbage. Some of the objects identified as garbage have been reclassified as not garbage. The garbage collector compacts the reclaimable memory and the special runtime thread empties the freachable queue, executing each object's Finalize method. Two GCs are required to reclaim memory used by objects that require finalization.

My question - Since two GCs are required to reclaim memory for objects that require finalization, if before the 2nd time collection happens, the object requiring finalization is strongly referenced again, does the garbage collector remove the object from the f-reachable queue so that its not garbage collected?

Upvotes: 2

Views: 211

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273179

does the garbage collector remove the object from the f-reachable queue so that its not garbage collected?

Yes.

It will always have been removed from fRreachable in the second run but when there is a new outside reference then it will not be collected. You need to use the GC.ReRegister() method to have it finalized again.

This whole pattern is called resurrection and is very rarely called for.

Upvotes: 4

Related Questions