Sam*_*uel 2 c# garbage-collection
我最初的问题是,在从例如Component继承的类中取消注册自引用事件处理程序是否很重要.他们提供一个Disposed活动,曾经可以去除东西.
但是我正在玩它并意识到一些奇怪的事情:从中导出System.ComponentModel.Component确实会阻止析构函数被调用(即使在应用程序结束时也是如此).
下面是一个显式使用GC.Collect强制收集的示例(仅测试,从不在prod代码中使用它)
using System;
using System.ComponentModel;
namespace CSharpEventHandlerLifetime
{
public class LongLiving : Component
{
public LongLiving()
{
Console.WriteLine("Creating object");
// Disposed += (sender, args) => Console.WriteLine("Disposed");
}
~LongLiving()
{
Console.WriteLine("Destructor called");
}
}
class Program
{
public static void _create()
{
using (var l = new LongLiving())
{
Console.WriteLine("Generation is {0}", GC.GetGeneration(l));
}
}
static void Main(string[] args)
{
_create();
GC.Collect(); // this should call dtor of LongLiving
Console.ReadKey(); // wait before end
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我完全删除继承的Component类(更改using为plain new)或用IDisposable替换它(并实现一些空的Dispose方法)时,我清楚地看到调用GC.Collect调用LongLiving的dtor.
我不明白这种行为,因为我至少期望在应用程序退出时进入~dtor但是从Component派生时它永远不会发生.
Component已经实现了Disposable模式,包括GC.SuppressFinalize完成时的调用.而Dispose被称为当using块退出.
所以对象是垃圾收集的,它永远不会被调用终结器.
如果你有任何清理工作,你应该覆盖Dispose(bool).
例如,这是Component小号Dispose方法:
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
Run Code Online (Sandbox Code Playgroud)