Пока «исследую» финализацию (читай: пробуя глупые вещи), я наткнулся на какое-то неожиданное поведение (по крайней мере, для меня).
Я ожидал, что метод Finalize не будет вызван, тогда как он вызывается дважды
class Program
{
static void Main(string[] args)
{
// The MyClass type has a Finalize method defined for it
// Creating a MyClass places a reference to obj on the finalization table.
var myClass = new MyClass();
// Append another 2 references for myClass onto the finalization table.
System.GC.ReRegisterForFinalize(myClass);
System.GC.ReRegisterForFinalize(myClass);
// There are now 3 references to myClass on the finalization table.
System.GC.SuppressFinalize(myClass);
System.GC.SuppressFinalize(myClass);
System.GC.SuppressFinalize(myClass);
// Remove the reference to the object.
myClass = null;
// Force the GC to collect the object.
System.GC.Collect(2, System.GCCollectionMode.Forced);
// The first call to obj's Finalize method will be discarded but
// two calls to Finalize are still performed.
System.Console.ReadLine();
}
}
class MyClass
{
~MyClass()
{
System.Console.WriteLine("Finalise() called");
}
}
Может ли кто-нибудь объяснить, является ли такое поведение намеренным, и если да, то почему?
Этот приведенный выше код был скомпилирован в режиме отладки x86 и запущен на CLR v4.
Большое спасибо