IDisposable 模式:当 GC.SupressFinalizer 时我应该取消变量吗

发布于 2024-12-22 05:17:38 字数 783 浏览 1 评论 0原文

每当我在 Dispose 方法中调用 GC.SupressFinalizer() 时, 我应该将 null 分配给所有实例成员以清理它们,否则它们在任何情况下都会被删除?

例如:

class test : IDisposable
{ 
    public int a = 10;
    public int b = 20;
    public int c = 39;

    private bool is_disposed = false;

    Dispose(bool shall_dispose)
    {
        if(!is_disposed && shall_dispose)
        {
            is_disposed = true;

            // shall I annulate a, b & c or they will be freed anyway??
            // a = null; ?, integers aint nullable 
            // b = null; ?
            // c = null; ?

            // or primitive types should't be annulated? only reference(heap) types?
        }
    }

    Dispose()
    {
        Dispose(true);
    }

    ~test()
    {
        Dispose(false);
    }
}

Whenever I call GC.SupressFinalizer() in the Dispose method,
should I assign null to all instance members to have them cleaned up, or they would be removed in any case ?

For example:

class test : IDisposable
{ 
    public int a = 10;
    public int b = 20;
    public int c = 39;

    private bool is_disposed = false;

    Dispose(bool shall_dispose)
    {
        if(!is_disposed && shall_dispose)
        {
            is_disposed = true;

            // shall I annulate a, b & c or they will be freed anyway??
            // a = null; ?, integers aint nullable 
            // b = null; ?
            // c = null; ?

            // or primitive types should't be annulated? only reference(heap) types?
        }
    }

    Dispose()
    {
        Dispose(true);
    }

    ~test()
    {
        Dispose(false);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩心不撩汉 2024-12-29 05:17:38

当对象不再可达时,GC.SupressFinalizer() 不会阻止对象被收集,它只会抑制对对象终结器的调用。因此,无论您是否分配 null,您的对象及其成员都将被收集。

GC.SupressFinalizer() will not stop your object from being collected when it is no longer reachable, it will just supress the call to the object's finalizer. So both your object and its members will be collected, no matter wether you assign null or not.

乱世争霸 2024-12-29 05:17:38

IDisposable.Dispose 的目的是通知一个对象不再需要它,并且应该告诉其他对象它们不再需要代表它执行任何操作(例如使文件保持打开状态、授予对某个文件的独占访问权限)。大多数时候,在一个对象被 Dispose 后不久,对它的任何引用都将被放弃,该对象将有资格进行垃圾回收,并且它持有的对其他对象的引用不会延迟它们的垃圾回收收藏。尽管如此,清除其他参考文献可能是个好主意。在调用 Dispose 后很长一段时间内,对象的引用完全有可能保留。此外,IDisposable 对象可能已经存在足够长的时间以到达垃圾收集器中的第 2 代,这意味着垃圾收集器可能需要一段时间才能再次查看它。如果它保存对最近创建的对象的引用,则将其引用设置为 null 可能会比其他情况更快地收集这些对象。

The purpose of IDisposable.Dispose is to notify an object that it won't be needed any more, and should tell other objects that they no longer need to do anything on its behalf (such as leave a file open, granting exclusive access to a block of memory, etc.) Most of the time, shortly after an object is Dispose'd, any references to it will be abandoned, the object will become eligible for garbage collection, and its holding references to other objects will not delay their garbage collection. Nonetheless, clearing out other references may be a good idea. It's entirely possible that a reference may remain to an object long after Dispose is called. Further, it's possible that the IDisposable object might have been around long enough to reach Generation 2 in the garbage collector, meaning that it could be awhile for the garbage collector even looks at it again. If it holds references to more-recently-created objects, setting its references to null may allow those objects to be collected sooner than they otherwise would.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文