<bdo id='ZidkO'></bdo><ul id='ZidkO'></ul>
  1. <small id='ZidkO'></small><noframes id='ZidkO'>

    <i id='ZidkO'><tr id='ZidkO'><dt id='ZidkO'><q id='ZidkO'><span id='ZidkO'><b id='ZidkO'><form id='ZidkO'><ins id='ZidkO'></ins><ul id='ZidkO'></ul><sub id='ZidkO'></sub></form><legend id='ZidkO'></legend><bdo id='ZidkO'><pre id='ZidkO'><center id='ZidkO'></center></pre></bdo></b><th id='ZidkO'></th></span></q></dt></tr></i><div id='ZidkO'><tfoot id='ZidkO'></tfoot><dl id='ZidkO'><fieldset id='ZidkO'></fieldset></dl></div>
    <tfoot id='ZidkO'></tfoot>

      <legend id='ZidkO'><style id='ZidkO'><dir id='ZidkO'><q id='ZidkO'></q></dir></style></legend>

    1. 我什么时候应该使用 GC.SuppressFinalize()?

      When should I use GC.SuppressFinalize()?(我什么时候应该使用 GC.SuppressFinalize()?)
    2. <tfoot id='HeKWV'></tfoot>

            <legend id='HeKWV'><style id='HeKWV'><dir id='HeKWV'><q id='HeKWV'></q></dir></style></legend>
              <bdo id='HeKWV'></bdo><ul id='HeKWV'></ul>
                <tbody id='HeKWV'></tbody>
            • <i id='HeKWV'><tr id='HeKWV'><dt id='HeKWV'><q id='HeKWV'><span id='HeKWV'><b id='HeKWV'><form id='HeKWV'><ins id='HeKWV'></ins><ul id='HeKWV'></ul><sub id='HeKWV'></sub></form><legend id='HeKWV'></legend><bdo id='HeKWV'><pre id='HeKWV'><center id='HeKWV'></center></pre></bdo></b><th id='HeKWV'></th></span></q></dt></tr></i><div id='HeKWV'><tfoot id='HeKWV'></tfoot><dl id='HeKWV'><fieldset id='HeKWV'></fieldset></dl></div>

              <small id='HeKWV'></small><noframes id='HeKWV'>

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

                问题描述

                在.NET中,什么情况下应该使用GC.SuppressFinalize()?

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

                使用这种方法有什么好处?

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

                推荐答案

                SuppressFinalize 只能由具有终结器的类调用.它通知垃圾收集器 (GC) this 对象已被完全清理.

                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
                            }
                            // Release unmanaged resources.
                            // Set large fields to null.                
                            disposed = true;
                        }
                    }
                
                    public void Dispose() // Implement IDisposable
                    {
                        Dispose(true);
                        GC.SuppressFinalize(this);
                    }
                
                    ~MyClass() // the finalizer
                    {
                        Dispose(false);
                    }
                }
                

                通常情况下,CLR 在创建对象时会使用终结器对对象进行标记(使创建它们的成本更高).SuppressFinalize 告诉 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.

                SuppressFinalize 优化并非微不足道,因为您的对象可以在终结器队列中等待很长时间.不要试图在其他对象上调用 SuppressFinalize 请注意.这是一个等待发生的严重缺陷.

                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.

                注意:有时编码人员会添加一个终结器来调试他们自己的 IDisposable 类的构建,以测试代码是否正确地处理了他们的 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.SuppressFinalize()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                C# namespace alias - what#39;s the point?(C# 命名空间别名 - 有什么意义?)
                Using Xpath With Default Namespace in C#(在 C# 中使用具有默认命名空间的 Xpath)
                IBM.Data.DB2.Core connection problems(IBM.Data.DB2.Core 连接问题)
                Generating an EDMX from a DB2 Database(从 DB2 数据库生成 EDMX)
                Datetime field overflow with IBM Data Server Client v9.7fp5(IBM Data Server Client v9.7fp5 的日期时间字段溢出)
                Using entity Framework with .NET Core and DB2(将实体框架与 .NET Core 和 DB2 结合使用)
              1. <tfoot id='Iuo4v'></tfoot>
                    <tbody id='Iuo4v'></tbody>
                  <legend id='Iuo4v'><style id='Iuo4v'><dir id='Iuo4v'><q id='Iuo4v'></q></dir></style></legend>
                    <bdo id='Iuo4v'></bdo><ul id='Iuo4v'></ul>

                    1. <i id='Iuo4v'><tr id='Iuo4v'><dt id='Iuo4v'><q id='Iuo4v'><span id='Iuo4v'><b id='Iuo4v'><form id='Iuo4v'><ins id='Iuo4v'></ins><ul id='Iuo4v'></ul><sub id='Iuo4v'></sub></form><legend id='Iuo4v'></legend><bdo id='Iuo4v'><pre id='Iuo4v'><center id='Iuo4v'></center></pre></bdo></b><th id='Iuo4v'></th></span></q></dt></tr></i><div id='Iuo4v'><tfoot id='Iuo4v'></tfoot><dl id='Iuo4v'><fieldset id='Iuo4v'></fieldset></dl></div>

                        <small id='Iuo4v'></small><noframes id='Iuo4v'>