Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

GC.SuppressFinalize Method

Requests that the system not call the finalizer for the specified object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Sub SuppressFinalize ( _
	obj As Object _
)
'Usage
Dim obj As Object 

GC.SuppressFinalize(obj)

Parameters

obj
Type: System.Object

The object that a finalizer must not be called for.

ExceptionCondition
ArgumentNullException

obj is Nothing.

This method sets a bit in the object header, which the system checks when calling finalizers. The obj parameter is required to be the caller of this method.

Objects that implement the IDisposable interface can call this method from the IDisposable.Dispose method to prevent the garbage collector from calling Object.Finalize on an object that does not require it.

The following example demonstrates how to use the SuppressFinalize method in a resource class to prevent a redundant garbage collection from being called.

Imports System
Imports System.ComponentModel

' The following example demonstrates how to use the  
' GC.SuppressFinalize method in a resource class to prevent 
' the clean-up code for the object from being called twice. 
Public Class DisposeExample

   ' A class that implements IDisposable. 
   ' By implementing IDisposable, you are announcing that  
   ' instances of this type allocate scarce resources. 
   Public Class MyResource
      Implements IDisposable
      ' Pointer to an external unmanaged resource. 
      Private handle As IntPtr
      ' Other managed resource this class uses. 
      Private component As Component
      ' Track whether Dispose has been called. 
      Private disposed As Boolean = False 


      ' The class constructor initializes the handle and component. 
      Public Sub New(ByVal handle As IntPtr)
         Me.handle = handle
      End Sub 


      ' Implement IDisposable. 
      ' Do not make this method virtual. 
      ' A derived class should not be able to override this method. 
      Public Overloads Sub Dispose() Implements IDisposable.Dispose
         Dispose(True)
         ' This object will be cleaned up by the Dispose method. 
         ' Therefore, you should call GC.SupressFinalize to 
         ' take this object off the finalization queue  
         ' and prevent finalization code for this object 
         ' from executing a second time.
         GC.SuppressFinalize(Me)
      End Sub 

      ' 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. 
      Private Overloads Sub Dispose(ByVal disposing As Boolean)
         ' Check to see if Dispose has already been called. 
         If Not Me.disposed Then 
            ' If disposing equals true, dispose all managed  
            ' and unmanaged resources. 
            If disposing Then 
               ' Dispose managed resources.
               component.Dispose()
            End If 

            ' Call the appropriate methods to clean up  
            ' unmanaged resources here. 
            ' If disposing is false,  
            ' only the following code is executed.
            CloseHandle(handle)
            handle = IntPtr.Zero
         End If
         disposed = True 
      End Sub 

      ' Use interop to call the method necessary   
      ' to clean up the unmanaged resource.
      <System.Runtime.InteropServices.DllImport("Kernel32")> _
      Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean]
      End Function 

      ' This finalizer will run only if the Dispose method  
      ' does not get called. 
      ' It gives your base class the opportunity to finalize. 
      ' Do not provide finalize methods in types derived from this class. 
      Protected Overrides Sub Finalize()
         ' Do not re-create Dispose clean-up code here. 
         ' Calling Dispose(false) is optimal in terms of 
         ' readability and maintainability.
         Dispose(False)
         MyBase.Finalize()
      End Sub 
   End Class 

Public Shared Sub Main()
   ' Insert code here to create 
   ' and use a MyResource object. 
End Sub 
End Class

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft