Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

GC.WaitForPendingFinalizers Method

Suspends the current thread until the thread processing the queue of finalizers has emptied that queue.

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

public static void WaitForPendingFinalizers ()
public static void WaitForPendingFinalizers ()
public static function WaitForPendingFinalizers ()

When the garbage collector finds objects that can be reclaimed, it checks each object to determine the object's finalization requirements. If an object implements a finalizer and has not disabled finalization by calling SuppressFinalize, the object is placed in a list of objects marked as ready for finalization. The garbage collector calls the Finalize methods for the objects in this list and removes the entries from the list. This method blocks until all finalizers have run to completion.

The thread on which finalizers are run is unspecified, so there is no guarantee that this method will terminate. However, this thread can be interrupted by another thread while the WaitForPendingFinalizers method is in progress. For example, you can start another thread that waits for a period of time then interrupts this thread if this thread is still suspended.

using System;

namespace WaitForPendingFinalizersExample
{
   class MyWaitForPendingFinalizersClass
   {
	// You can increase this number to fill up more memory.
	const int numMfos = 1000;
	// You can increase this number to cause more
	// post-finalization work to be done.
	const int maxIterations = 100;

	static void Main(string[] args)
	{
	   MyFinalizeObject mfo = null;
         
	   // Create and release a large number of objects
	   // that require finalization.
	   for(int j = 0; j < numMfos; j++)
	   {
	      mfo = new MyFinalizeObject();
	   }
         
	   //Release the last object created in the loop.
	   mfo = null;

	   //Force garbage collection.
	   GC.Collect();
         
	   // Wait for all finalizers to complete before continuing.
	   // Without this call to GC.WaitForPendingFinalizers, 
	   // the worker loop below might execute at the same time 
	   // as the finalizers.
	   // With this call, the worker loop executes only after
	   // all finalizers have been called.
	   GC.WaitForPendingFinalizers();

	   // Worker loop to perform post-finalization code.
	   for(int i = 0; i < maxIterations; i++)
	   {
	      Console.WriteLine("Doing some post-finalize work");
	   }
	}
   }

   class MyFinalizeObject
   {
	// Make this number very large to cause the finalizer to
	// do more work.
	private const int maxIterations = 10000;
      
	~MyFinalizeObject()
	{
	   Console.WriteLine("Finalizing a MyFinalizeObject");
			
	   // Do some work.
	   for(int i = 0; i < maxIterations; i++)
	   {
	      // This method performs no operation on i, but prevents 
	      // the JIT compiler from optimizing away the code inside 
	      // the loop.
	      GC.KeepAlive(i);
	   }
        }
    }
}

package WaitForPendingFinalizersExample ; 

import System.* ;

class MyWaitForPendingFinalizersClass
{
    // You can increase this number to fill up more memory.
    private static final int numMfos = 1000;

    // You can increase this number to cause more
    // post-finalization work to be done.
    private static final int maxIterations = 100;

    public static void main(String[] args)
    {
        MyFinalizeObject mfo = null;

        // Create and release a large number of objects
        // that require finalization.
        for (int j = 0; j < numMfos; j++) {
            mfo = new MyFinalizeObject();
        }

        //Release the last object created in the loop.
        mfo = null;

        //Force garbage collection.
        GC.Collect();

        // Wait for all finalizers to complete before continuing.
        // Without this call to GC.WaitForPendingFinalizers, 
        // the worker loop below might execute at the same time 
        // as the finalizers.
        // With this call, the worker loop executes only after
        // all finalizers have been called.
        GC.WaitForPendingFinalizers();

        // Worker loop to perform post-finalization code.
        for (int i = 0; i < maxIterations; i++) {
            Console.WriteLine("Doing some post-finalize work");
        }
    } //main
} //MyWaitForPendingFinalizersClass

class MyFinalizeObject
{
    // Make this number very large to cause the finalizer to
    // do more work.
    private int maxIterations = 10000;

    public void finalize()
    {
        Console.WriteLine("Finalizing a MyFinalizeObject");

        // Do some work.
        for (int i = 0; i < maxIterations; i++) {
            // This method performs no operation on i, but prevents 
            // the JIT compiler from optimizing away the code inside 
            // the loop.
            GC.KeepAlive(new Integer(i));
        }

        try {
            super.finalize();
        }
        catch (System.Exception e) {
            Console.WriteLine(e.get_Message());
        }
    } //finalize
} //MyFinalizeObject   

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.