Is a finalizable object with GC.SuppressFinalize the same as a normal unfinalizable object? The code below seems to prove they're treated differently, both on .NET 2 and 4:
class Class1 {
public Class1()
{
GC.SuppressFinalize(this);
}
//~Class1() { }
}
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i=0; i<100000000; i++)
{
new Class1();
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}
}
Adding the finalizer, but not changing anything else, causes the code to take far far longer (12601 ms compared to 889 ms).
I thought SuppressFinalize set a bit in the object header making the GC treat the object the same as a non-finalizable object, but this does not seem to be the case. So what's going on? What is different between a non-finalizable object and a finalizable object with GC.SuppressFinalize called on it?
GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect();
after all thenew
s. There was no appreciable difference with vs. without the (suppressed) finalizer. – Demigod