zoukankan      html  css  js  c++  java
  • C# 托管资源和非托管资源

    asp.net内存回收与Dispose﹐Close﹐Fina...

    http://www.163ns.com/zixun/post/4565.html

    C#内存回收与Dispose﹐Close﹐Finalize方法

    http://www.cnblogs.com/whyandinside/articles/1567130.html

    Finalize()
     
    This method is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to SuppressFinalize. During shutdown of an application domain, Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible. Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as ReRegisterForFinalize and GC.SuppressFinalize has not been subsequently called.

    Every implementation of Finalize in a derived type must call its base type's implementation of Finalize. This is the only case in which application code is allowed to call Finalize.

    Finalize operations have the following limitations:

    • The exact time when the finalizer executes during garbage collection is undefined. Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose method.
    • The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other. That is, if Object A has a reference to Object B and both have finalizers, Object B might have already finalized when the finalizer of Object A starts.
    • The thread on which the finalizer is run is unspecified.

    The Finalize method might not run to completion or might not run at all in the following exceptional circumstances:

    • Another finalizer blocks indefinitely (goes into an infinite loop, tries to obtain a lock it can never obtain and so on). Because the runtime attempts to run finalizers to completion, other finalizers might not be called if a finalizer blocks indefinitely.
    • The process terminates without giving the runtime a chance to clean up. In this case, the runtime's first notification of process termination is a DLL_PROCESS_DETACH notification.

    The runtime continues to Finalize objects during shutdown only while the number of finalizable objects continues to decrease.

    If Finalize or an override of Finalize throws an exception, and the runtime is not hosted by an application that overrides the default policy, the runtime terminates the process and no active try-finally blocks or finalizers are executed. This behavior ensures process integrity if the finalizer cannot free or destroy resources.


    Dispose()
    A type's Disposemethod should release all the resources that it owns. It should also release all resources owned by its base types by calling its parent type's Dispose method. The parent type's Dispose method should release all resources that it owns and in turn call its parent type's Dispose method, propagating this pattern through the hierarchy of base types. To help ensure that resources are always cleaned up appropriately, a Dispose method should be callable multiple times without throwing an exception.

    There is no performance benefit in implementing the Dispose method on types that use only managed resources (such as arrays) because they are automatically reclaimed by the garbage collector. Use the Dispose method primarily on managed objects that use native resources and on COM objects that are exposed to the .NET Framework. Managed objects that use native resources (such as the FileStream class) implement the IDisposable interface. 
    A Dispose method should call the SuppressFinalize method for the object it is disposing. If the object is currently on the finalization queue, SuppressFinalize prevents its Finalize method from being called. Remember that executing a Finalize method is costly to performance. If your Dispose method has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method.

     

    // Design pattern for the base class.
    // By implementing IDisposable, you are announcing that instances
    // of this type allocate scarce resources.
    public class BaseResource: IDisposable
    {
       // Pointer to an external unmanaged resource.
       private IntPtr handle;
       // Other managed resource this class uses.
       private Component Components;
       // Track whether Dispose has been called.
       private bool disposed = false;

       // Constructor for the BaseResource object.
       public BaseResource()
       {
          // Insert appropriate constructor code here.
       }

       // Implement IDisposable.
       // Do not make this method virtual.
       // A derived class should not be able to override this method.
       public void Dispose()
       {
          Dispose(true);
          // Take yourself off the Finalization queue 
          // to prevent finalization code for this object
          // from executing a second time.
          GC.SuppressFinalize(this);
       }

       // Dispose(bool disposing) executes in two distinct scenarios.
       // If disposing equals true, the method has been called directly
       // or indirectly by a user's code. Managed and unmanaged resources
       // can be disposed.
       // If disposing equals false, the method has been called by the 
       // runtime from inside the finalizer and you should not reference 
       // other objects. Only unmanaged resources can be disposed.
       protected virtual void Dispose(bool disposing)
       {
          // Check to see if Dispose has already been called.
          if(!this.disposed)
          {
             // If disposing equals true, dispose all managed 
             // and unmanaged resources.
             if(disposing)
             {
                // Dispose managed resources.
                Components.Dispose();
             }
             // Release unmanaged resources. If disposing is false, 
             // only the following code is executed.
             CloseHandle(handle);
             handle = IntPtr.Zero;
             // Note that this is not thread safe.
             // Another thread could start disposing the object
             // after the managed resources are disposed,
             // but before the disposed flag is set to true.
             // If thread safety is necessary, it must be
             // implemented by the client.

          }
          disposed = true;         
       }

       // Use C# destructor syntax for finalization code.
       // This destructor will run only if the Dispose method 
       // does not get called.
       // It gives your base class the opportunity to finalize.
       // Do not provide destructors in types derived from this class.
       ~BaseResource()      
       {
          // Do not re-create Dispose clean-up code here.
          // Calling Dispose(false) is optimal in terms of
          // readability and maintainability.
          Dispose(false);
       }

       // Allow your Dispose method to be called multiple times,
       // but throw an exception if the object has been disposed.
       // Whenever you do something with this class, 
       // check to see if it has been disposed.
       public void DoSomething()
       {
          if(this.disposed)
          {
             throw new ObjectDisposedException();
          }
       }
    }

    Close()



    GC.KeepAlive()

    KeepAlive 方法的目的是确保对对象的引用存在,该对象有被垃圾回收器过早回收的危险。这种现象可能发生的一种常见情形是,当在托管代码或数据中已没有对该对象的引用,但该对象仍然在非托管代码(如 Win32 API、非托管 DLL 或使用 COM 的方法)中使用。另一种过早发生垃圾回收的情形是,在一个方法中创建并使用一个对象。此时,当对对象的某个成员的调用仍在执行时,可能会对该对象进行回收。 
    此方法引用 obj,从而使该对象从例程开始到调用此方法的那个位置(按执行顺序)均不符合进行垃圾回收的条件。在 obj 必须可用的指令范围的结尾(而不是开头)编写此方法的代码。
    KeepAlive 方法除了延长作为参数传递的对象的生存期之外,不会执行任何操作,也会不产生任何其他副作用。


    。。
    Summary
    类型的 Dispose 方法应释放它拥有的所有资源。它还应该通过调用其父类型的 Dispose 方法释放其基类型拥有的所有资源。该父类型的 Dispose 方法应该释放它拥有的所有资源并同样也调用其父类型的 Dispose 方法,从而在整个基类型层次结构中传播此模式。若要确保始终正确地清理资源,Dispose 方法应该可以被多次调用而不引发任何异常。
    Finalize (在C#中就是析构函数,在VB中是使用Finalize关键字)和 Dispose 都是释放资源,Finalize 隐式释放资源,Dispose 显式释放资源。Finalize 是对象不可访问后自动被调用的,Dispose 是类使用者调用的。
    对于类设计者:

    1. Finalize 和 Dispose 释放的资源应该相同(除托管资源外),这样即使类使用者在没有调用 Dispose 的情况下,资源也会在 Finalize 中得到释放。
    2. Dispose 中应该调用 GC.SuppressFinalize 方法,这样类使用者调用了 Dispose 后,就不会自动调用 Finalize 了,因为调用 Dispose 后没有必要再执行 Finalize。
    3. Finalize 不应为 public。

    对于类使用者:
    有 Dispose 方法存在时,应该调用它,因为 Finalize 释放资源通常是很慢的。也就是说,对于类使用者,我们只需要调用 Dispose 就可以了,不需要关注 Finalize,因为 Finalize 通常不是 public 的。如果类使用者没有调用 Dispose 方法,Finalize 是释放资源的最后防线。当然这些都是建立在类设计者遵照上述规则设计的前提下。
    GC.KeepAlive 延长对象的生存周期,即使现在没有引用指向这个对象。

    Reference:
    http://msdn.microsoft.com/zh-cn/library/system.object.finalize(VS.80).aspx

    http://hi.baidu.com/pcclcbafihfmowr/item/345a78d3b35bdf1d20e250c4

    http://blog.csdn.net/zlwzlwzlw/article/details/7918633

     托管资源指的是.NET可以自动进行回收的资源,主要是指托管堆上分配的内存资源。托管资源的回收工作是不需要人工干预的,有.NET运行库在合适调用垃圾回收器进行回收。

             非托管资源指的是.NET不知道如何回收的资源,最常见的一类非托管资源是包装操作系统资源的对象,例如文件,窗口,网络连接,数据库连接,画刷,图标等。这类资源,垃圾回收器在清理的时候会调用Object.Finalize()方法。默认情况下,方法是空的,对于非托管对象,需要在此方法中编写回收非托管资源的代码,以便垃圾回收器正确回收资源。

             在.NET中,Object.Finalize()方法是无法重载的,编译器是根据类的析构函数来自动生成Object.Finalize()方法的,所以对于包含非托管资源的类,可以将释放非托管资源的代码放在析构函数。

             注意,不能在析构函数中释放托管资源,因为析构函数是有垃圾回收器调用的,可能在析构函数调用之前,类包含的托管资源已经被回收了,从而导致无法预知的结果。

             本来如果按照上面做法,非托管资源也能够由垃圾回收器进行回收,但是非托管资源一般是有限的,比较宝贵的,而垃圾回收器是由CRL自动调用的,这样就无法保证及时的释放掉非托管资源,因此定义了一个Dispose()方法,让使用者能够手动的释放非托管资源。Dispose()方法释放类的托管资源和非托管资源,使用者手动调用此方法后,垃圾回收器不会对此类实例再次进行回收。Dispose()方法是由使用者调用的,在调用时,类的托管资源和非托管资源肯定都未被回收,所以可以同时回收两种资源。

             Microsoft为非托管资源的回收专门定义了一个接口:IDisposable,接口中只包含一个Dispose()方法。任何包含非托管资源的类,都应该继承此接口。

             在一个包含非托管资源的类中,关于资源释放的标准做法是:

             (1)     继承IDisposable接口;

             (2)     实现Dispose()方法,在其中释放托管资源和非托管资源,并将对象本身从垃圾回收器中移除(垃圾回收器不在回收此资源);

             (3)     实现类析构函数,在其中释放非托管资源。

             在使用时,显示调用Dispose()方法,可以及时的释放资源,同时通过移除Finalize()方法的执行,提高了性能;如果没有显示调用Dispose()方法,垃圾回收器也可以通过析构函数来释放非托管资源,垃圾回收器本身就具有回收托管资源的功能,从而保证资源的正常释放,只不过由垃圾回收器回收会导致非托管资源的未及时释放的浪费。

             在.NET中应该尽可能的少用析构函数释放资源。在没有析构函数的对象在垃圾处理器一次处理中从内存删除,但有析构函数的对象,需要两次,第一次调用析构函数,第二次删除对象。而且在析构函数中包含大量的释放资源代码,会降低垃圾回收器的工作效率,影响性能。所以对于包含非托管资源的对象,最好及时的调用Dispose()方法来回收资源,而不是依赖垃圾回收器。

             上面就是.NET中对包含非托管资源的类的资源释放机制,只要按照上面要求的步骤编写代码,类就属于资源安全的类。

             下面用一个例子来总结一下.NET非托管资源回收机制:

             Public class BaseResource:IDisposable

             {

                      PrivateIntPtr handle; // 句柄,属于非托管资源

                      PrivateComponet comp; // 组件,托管资源

                      Privateboo isDisposed = false; // 是否已释放资源的标志

            

                      PublicBaseResource

                      {

                      }

            

                      //实现接口方法

                      //由类的使用者,在外部显示调用,释放类资源

                      Publicvoid Dispose()

                      {

                                Dispose(true);// 释放托管和非托管资源

                               

                                //将对象从垃圾回收器链表中移除,

                                // 从而在垃圾回收器工作时,只释放托管资源,而不执行此对象的析构函数

                                GC.SuppressFinalize(this);

                      }

            

                      //由垃圾回收器调用,释放非托管资源

                      ~BaseResource()

                      {

                                Dispose(false);// 释放非托管资源

                      }

            

                      //参数为true表示释放所有资源,只能由使用者调用

                      //参数为false表示释放非托管资源,只能由垃圾回收器自动调用

                      //如果子类有自己的非托管资源,可以重载这个函数,添加自己的非托管资源的释放

                      //但是要记住,重载此函数必须保证调用基类的版本,以保证基类的资源正常释放

                      Protectedvirtual void Dispose(bool disposing)

                      {

                                If(!this.disposed)// 如果资源未释放 这个判断主要用了防止对象被多次释放

                                {

                                         If(disposing)

                                         {

                                                  Comp.Dispose();// 释放托管资源

                                         }

                               

                                         closeHandle(handle);// 释放非托管资源

                                         handle= IntPtr.Zero;

                                }

                                this.disposed= true; // 标识此对象已释放

                      }

             }
             

             析构函数只能由垃圾回收器调用。

             Despose()方法只能由类的使用者调用。

             在C#中,凡是继承了IDisposable接口的类,都可以使用using语句,从而在超出作用域后,让系统自动调用Dispose()方法。
             一个资源安全的类,都实现了IDisposable接口和析构函数。提供手动释放资源和系统自动释放资源的双保险。

  • 相关阅读:
    学习web前端要去一线就业吗
    程序员什么时候该考虑跳槽
    前端工程师应该具备怎样的一种技术水平
    如何掌握学习移动端Web页面布局
    如何优化Web前端技术开发生态体系
    想进名企大厂?阿里程序员给你三点建议
    对即将入职前端工作的新人有哪些建议?
    Java基础学习之快速掌握Session和cookie
    Java入门学习之JDK介绍与初次编程实现
    Java编译的运行机制初步讲解
  • 原文地址:https://www.cnblogs.com/jingzhishen/p/3815493.html
Copyright © 2011-2022 走看看