Finalizing Your Code Properly

Classes with finalizers or destructors are more expensive to clean up than objects without destructors. This is because the garbage collector has to make two passes for objects with destructors: the first pass executes the destructor, and the second pass cleans up the object.

The garbage collector has two structures to manage objects with destructors: a finalization queue and a F-reachable queue. When an object with a destructor becomes unusable, a pointer to it is added to the finalization queue. This indicates that the object must have its destructor called before it's cleaned up. During a garbage collection, all entries in the finalization queue are moved to the F-reachable queue; their destructors are called; ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.