This documentation is archived and is not being maintained.

GC::SuppressFinalize Method

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

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

static void SuppressFinalize(
	Object^ obj


Type: System::Object
The object that a finalizer must not be called for.


obj is nullptr.

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.

#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;

// The following example demonstrates how to use the
// GC::SuppressFinalize method to prevent finalization of an
// object that has been disposed. For C++, the call to 
// GC::SuppressFinalize is not explicit; it is emitted by the
// compiler, in the Dispose() method. The comments in this code
// example explain where that call occurs and its part in the
// overall Dispose/Finalize pattern.
// The code example demonstrates how to create a class that 
// implements the IDisposable interface and the IDisposable.Dispose
// method. By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources, which can be
// released by calling Dispose() when the programmer is finished 
// with the object or which will be released when the finalizer
// is executed. 
// Note that for This code example 
public ref class MyResource: public IDisposable

   // Pointer to an external unmanaged resource.
   IntPtr handle;

   // A managed resource this class uses.
   Component^ component;

   // Track whether Dispose has been called.
   bool disposed;

   // The class constructor.
   MyResource( IntPtr handle, Component^ component )
      this->handle = handle;
      this->component = component;
      disposed = false;

   // In managed code, the destructor contains code to clean up managed
   // resources. This method is called by Dispose(bool disposing), 
   // which the C++ compiler emits for you when you implement the 
   // destructor; it is only called if disposing == true -- that is, 
   // if the user has called Dispose(). (In C++, the user calls 
   // ~MyResource(), which the compiler emits as a call to Dispose().)
   // The emitted Dispose() method calls GC::SuppressFinalize( this )
   // for you, so there is no need to call it here.
      // Dispose of managed resources.

      disposed = true;

   // Use interop to call the method necessary to clean up the 
   // unmanaged resource.
   static Boolean CloseHandle( IntPtr handle );

   // Note: The Dispose(bool disposing) method emitted by the 
   // compiler executes in two distinct scenarios. If disposing ==
   // true, the method has been called directly or indirectly by a 
   // user's code. Managed and unmanaged resources can be disposed,
   // so both ~MyResource() and !MyResource() are called.
   // 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, so
   // only !MyResource() is called.

   // This destructor runs when the Dispose method gets called 
   // explicitly, and also when (and if) the Finalizer is run.
      // Call the appropriate methods to clean up unmanaged 
      // resources here. If disposing is false when Dispose(bool,
      // disposing) is called, only the following code is executed.
      CloseHandle( handle );
      handle = IntPtr::Zero;


void main()
   // Insert code here to create and use the MyResource object.
   MyResource^ mr = gcnew MyResource((IntPtr) 42, (Component^) gcnew Button());

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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