Yes, I know how to use it GC.SuppressFinalize()- he explained here . I have read many times that using GC.SuppressFinalize()removes an object from the finalization queue, and it is considered that this is good because it frees the GC from additional work by invoking the finalizer.
So, I created this (mostly useless) code where the class implements IDisposable, as in the related answer:
public class MyClass : IDisposable
{
~MyClass()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
System.Threading.Thread.Sleep(0);
disposed = true;
}
}
}
Sleep(0) . , - - Dispose() , - " " .
, :
var start = DateTime.UtcNow;
var objs = new List<Object>();
for (int i = 0; i < 1000 * 1000 * 10; i++)
{
using (var obj = new MyClass())
{
objs.Add(obj);
}
}
objs = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
var duration = (DateTime.UtcNow - start).TotalMilliseconds;
Console.WriteLine(duration.ToString());
, , , List.
, , 12.01 (Release, ). GC.SuppressFinalize() , 13.99 .
, GC.SuppressFinalize(), 14,1% . , , GC ( , ?) 14%.
, , , , .
- ? , GC.SuppressFinalize()?