清除 Unmanaged 資源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. 但是,當您建立包含 Unmanaged 資源的物件時,必須在應用程式使用完這些資源後明確地釋放它們。However, when you create objects that include unmanaged resources, you must explicitly release those resources when you finish using them in your app. 最常見的 Unmanaged 資源類型就是包裝作業系統資源的物件,例如檔案、視窗、網路連接或資料庫連接都屬於這類資源。The most common types of unmanaged resource 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 實作以便將 Unmanaged 資源進行決定性的釋放。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 方法會立即釋放 Unmanaged 資源。The Dispose method immediately releases the unmanaged resources.

  • 提供的用途是在您的類型消費者忘記呼叫 Dispose 時釋放 Unmanaged 資源。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:

    • 使用安全控制代碼包裝您的 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 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. 不過,由於物件最終處理可能是複雜且容易發生錯誤的作業,因此建議您使用安全控制代碼,而不要提供您自己的完成項。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 實作來釋放 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 statement or the Visual Basic Using statement to do this.

參考資料Reference

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.