清理非托管资源Cleaning Up Unmanaged Resources

对于应用创建的大多数对象,可以依赖 .NET 的垃圾回收器来处理内存管理。For the majority of the objects that your app creates, you can rely on .NET's garbage collector to handle memory management. 但是,如果创建包括非托管资源的对象,则当你在应用中使用完非托管资源后,必须显式释放这些资源。However, when you create objects that include unmanaged resources, you must explicitly release those resources when you finish using them in your app. 最常用的非托管资源类型是包装操作系统资源的对象,如文件、窗口、网络连接或数据库连接。The most common types of unmanaged resource are objects that wrap operating system resources, such as files, windows, network connections, or database connections. 虽然垃圾回收器可以跟踪封装非托管资源的对象的生存期,但无法了解如何发布并清理这些非托管资源。Although the garbage collector is able to track the lifetime of an object that encapsulates an unmanaged resource, it doesn't know how to release and clean up the unmanaged resource.

如果你的类型使用非托管资源,则应执行以下操作:If your types use unmanaged resources, you should do the following:

  • 实现清理模式Implement the dispose pattern. 这要求你提供 IDisposable.Dispose 实现以启用非托管资源的确定性释放。This requires that you provide an IDisposable.Dispose implementation to enable the deterministic release of unmanaged resources. 当不再需要此对象(或其使用的资源)时,类型使用者可调用 DisposeA consumer of your type calls Dispose when the object (and the resources it uses) is no longer needed. Dispose 方法立即释放非托管资源。The Dispose method immediately releases the unmanaged resources.

  • 在类型使用者忘记调用 Dispose 的情况下,准备释放非托管资源。Provide for your unmanaged resources to be released in the event that a consumer of your type forgets to call Dispose. 有两种方法可以实现此目的:There are two ways to do this:

    • 使用安全句柄包装非托管资源。Use a safe handle to wrap your unmanaged resource. 这是推荐采用的方法。This is the recommended technique. 安全句柄派生自 System.Runtime.InteropServices.SafeHandle 类并包含可靠的 Finalize 方法。Safe handles are derived from the System.Runtime.InteropServices.SafeHandle class and include a robust Finalize method. 在使用安全句柄时,只需实现 IDisposable 接口并在 Dispose 实现中调用安全句柄的 IDisposable.Dispose 方法。When you use a safe handle, you simply implement the IDisposable interface and call your safe handle's Dispose method in your IDisposable.Dispose implementation. 如果未调用安全句柄的 Dispose 方法,则垃圾回收器将自动调用安全句柄的终结器。The safe handle's finalizer is called automatically by the garbage collector if its Dispose method is not called.

      - 或 -—or—

    • 重写 Object.Finalize 方法。Override the Object.Finalize method. 当类型使用者无法调用 IDisposable.Dispose 以确定性地释放非托管资源时,终止会启用对非托管资源的非确定性释放。Finalization enables the non-deterministic release of unmanaged resources when the consumer of a type fails to call IDisposable.Dispose to dispose of them deterministically. 但是,由于对象终止是一项复杂且易出错的操作,建议你使用安全句柄而不是提供你自己的终结器。However, because object finalization can be a complex and error-prone operation, we recommend that you use a safe handle instead of providing your own finalizer.

然后,类型使用者可直接调用 IDisposable.Dispose 实现以释放非托管资源使用的内存。Consumers of your type can then call your IDisposable.Dispose implementation directly to free memory used by unmanaged resources. 在正确实现 Dispose 方法时,安全句柄的 Finalize 方法或 Object.Finalize 方法的重写会在未调用 Dispose 方法的情况下阻止清理资源。When you properly implement a Dispose method, either your safe handle's Finalize method or your own override of the Object.Finalize method becomes a safeguard to clean up resources in the event that the Dispose method is not called.

本节内容In This Section

实现 Dispose 方法Implementing a Dispose Method
介绍了如何实现清理模式来清理非托管资源。Describes how to implement the dispose pattern for releasing unmanaged resources.

使用实现 IDisposable 的对象Using Objects That Implement IDisposable
描述类型使用者如何确保调用其 Dispose 实现。Describes how consumers of a type ensure that its Dispose implementation is called. 建议使用 C# using 语句或 Visual Basic Using 语句来执行此操作。We recommend using the C# using statement or the Visual Basic Using statement to do this.

参考Reference

System.IDisposable
定义用于释放非托管资源的 Dispose 方法。Defines the Dispose method for releasing unmanaged resources.

Object.Finalize
如果 Dispose 方法未释放非托管资源,则准备对象终止。Provides for object finalization if unmanaged resources are not released by the Dispose method.

GC.SuppressFinalize
取消终止。Suppresses finalization. 通常,从 Dispose 方法调用此方法来阻止执行终结器。This method is customarily called from a Dispose method to prevent a finalizer from executing.