You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Finalizer no longer called in .NET Core 2.0 after GC.Collect / GC.WaitForPendingFinalizers #1115
Update: I also reported this issue here, since that repo might be more appropriate. I'll close either this one or the other once I know for sure.
Finalizer no longer called in .NET Core 2.0 after GC.Collect / GC.WaitForPendingFinalizers
As of .NET Core 2.0, an object's finalizer is no longer called when calling GC.Collect and GC.WaitForPendingFinalizers.
General
To be clear: the following code works as advertised in .NET Framework 4.7 and earlier; .NET Core 1.1 and earlier; but not in .NET Core 2.0:
[TestClass]publicclassGarbageCollectionTest{[TestMethod][Description("Asserts that an un-rooted object's finalizer will be called.")]publicvoidGarbageCollectionTest01(){// Instantiate a spy to record that the Foo finalizer is called.varspy=newSpy();// Instantiate a new Foo.varfoo=newFoo(spy);// Unroot foo.foo=null;// Force a collection.GC.Collect(0,GCCollectionMode.Forced,blocking:true);// Wait for all finalizers to have executed.GC.WaitForPendingFinalizers();// Assert that Foo's finalizer is called.// WARNING: This assertion fails in .NET Core 2.0, but works in .NET Core 1.1 and .NET Framework 4.7 and before.Assert.IsTrue(spy.FinalizerIsCalled);}privateclassSpy{publicboolFinalizerIsCalled{get;set;}}privateclassFoo{~Foo(){spy.FinalizerIsCalled=true;}publicFoo(Spyspy){this.spy=spy;}privatereadonlySpyspy;}}
Unit test output:
To demonstrate the issue, I created a unit test project for the following frameworks which all share the same .cs file:
Activity
jkotas commentedon Nov 24, 2017
CoreCLR repo is the right place for this issue.