Objects with finalizers cannot be collected as part of a single garbage collection procedure. Such objects are moved to the f-reachable
queue and remain there until finalizers are output. Only then can they be collected in garbage.
The following code is better, but you should not rely on it anyway:
GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect();
Also, throwing exceptions in the finalizer seems too cruel for me, even for testing purposes.
In addition, there is an interesting side effect of finalizers: an object with a finalizer can still "resurrect" itself (effectively prevent garbage collection) if it stores the this
link in finalizer (assigns it some static variable).
source share