什么时候应该使用GC.Sup pressFinalize()? [英] When should I use GC.SuppressFinalize()?

查看:282
本文介绍了什么时候应该使用GC.Sup pressFinalize()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,在哪些情况下我应该使用 GC.Sup pressFinalize()

In .NET, under which circumstances should I use GC.SuppressFinalize()?

什么优势(S)不使用这种方法给我吗?

What advantage(s) does using this method give me?

推荐答案

燮pressFinalize只能由具有终结一类调用。它的通知垃圾收集器(GC)的对象清理全面。

SuppressFinalize should only be called by a class that has a finalizer. It's informing the Garbage Collector (GC) that this object was cleaned up fully.

建议IDisposable模式,当你有一个终结是:

The recommended IDisposable pattern when you have a finalizer is:

public class MyClass : IDisposable
{
    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
                // called via myClass.Dispose(). 
                // OK to use any private object references
            }

            disposed = true;
        }
    }

    public void Dispose() // Implement IDisposable
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    ~MyClass() // the finalizer
    {
        Dispose(false);
    }
}

通常情况下,CLR保持标签上有一个终结的对象被创建时,他们(使他们更昂贵的创建)。 SUP pressFinalize告诉该对象被正确清理,并不需要去到终结队列GC上。它看起来像一个C ++的析构函数,但不作为任何类似之一。

Normally, the CLR keeps tabs on objects with a finalizer when they are created (making them more expensive to create). SuppressFinalize tells the GC that the object was cleaned up properly and doesn't need to go onto the finalizer queue. It looks like a C++ destructor, but doesn't act anything like one.

在燮pressFinalize优化是不平凡的,因为你的对象能活很长时间等待的终结队列。不要试图打电话给燮pressFinalize其他对象提个醒。这是一个严重的缺陷等待发生。

The SuppressFinalize optimization is not trivial, as your objects can live a long time waiting on the finalizer queue. Don't be tempted to call SuppressFinalize on other objects mind you. That's a serious defect waiting to happen.

设计导则告诉我们,终结是没有必要的,如果你的对象实现了IDisposable,但如果你有一个终结,你应该实现IDisposable,让你的类的确定性清理。

Design guidelines inform us that a finalizer isn't necessary if your object implements IDisposable, but if you have a finalizer you should implement IDisposable to allow deterministic cleanup of your class.

在大多数情况下,你应该能够逃脱IDisposable接口清理资源。您应该只需要一个终结,当你的对象保存到非托管资源,你需要确保这些资源的清理。

Most of the time you should be able to get away with IDisposable to clean up resources. You should only need a finalizer when your object holds onto unmanaged resources and you need to guarantee those resources are cleaned up.

注:有时候codeRS将增加一个终结调试建立自己的IDisposable班,以测试code配置了自己的IDisposable对象正常。

Note: Sometimes coders will add a finalizer to debug builds of their own IDisposable classes in order to test that code has disposed their IDisposable object properly.

    public void Dispose() // Implement IDisposable
    {
        Dispose(true);
    #if DEBUG
        GC.SuppressFinalize(this);
    #endif
    }

    #if DEBUG
    ~MyClass() // the finalizer
    {
        Dispose(false);
    }
    #endif

这篇关于什么时候应该使用GC.Sup pressFinalize()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆