清除 Unmanaged 資源Cleaning up unmanaged resources

針對您的應用程式所建立的大部分物件,您可以依賴.net 垃圾收集行程來處理記憶體管理。For the majority of the objects that your app creates, you can rely on the .NET 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. 最常見的非受控資源類型是包裝作業系統資源的物件,例如檔案、windows、網路連接或資料庫連接。The most common types of unmanaged resources are objects that wrap operating system resources, such as files, windows, network connections, or database connections. 雖然記憶體回收行程能夠追蹤封裝 Unmanaged 資源的物件存留期,但是它並不知道如何釋放和清除 Unmanaged 資源。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.

如果您的類型使用 Unmanaged 資源,則應該執行下列作業: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. 當您不再 Dispose 需要物件(及其使用的資源)時,您的型別取用者會呼叫。A consumer of your type calls Dispose when the object (and the resources it uses) are no longer needed. Dispose 方法會立即釋放 Unmanaged 資源。The Dispose method immediately releases the unmanaged resources.

  • 如果您的型別取用者忘記呼叫 Dispose ,請提供一種方式來釋放非受控資源。In the event that a consumer of your type forgets to call Dispose, provide a way for your unmanaged resources to be released. 作法有二:There are two ways to do this:

    • 使用安全控制代碼包裝您的 Unmanaged 資源。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 abstract 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 以決定性的方式處理 Unmanaged 資源時,進行非決定性的 Unmanaged 資源釋放。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. 藉由寫方法來定義完成項 Object.FinalizeDefine a finalizer by overriding the Object.Finalize method.

    警告

    不過,因為物件結束可能是複雜且容易出錯的作業,所以建議您使用安全控制碼,而不要提供自己的完成項。However, because object finalization can be a complex and an error-prone operation, we recommend that you use a safe handle instead of providing your own finalizer.

這樣類型的消費者就可以直接呼叫您的 IDisposable.Dispose 實作來釋放 Unmanaged 資源所使用的記憶體。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 的物件描述類型的取用者如何確保其 Dispose 實作為呼叫。Using objects that implement IDisposable describes how consumers of a type ensure that its Dispose implementation is called. 我們建議使用 c # using (或 Visual Basic Using )語句來執行此動作。We recommend using the C# using (or the Visual Basic Using) statement to do this.

參考Reference

類型/成員Type / Member 描述Description
System.IDisposable 定義用於釋放 Unmanaged 資源的 Dispose 方法。Defines the Dispose method for releasing unmanaged resources.
Object.Finalize 提供的用途是在 Dispose 方法未釋放 Unmanaged 資源時,進行物件最終處理。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.