本文整理匯總了C#中System.GC.WaitForPendingFinalizers方法的典型用法代碼示例。如果您正苦於以下問題:C# GC.WaitForPendingFinalizers方法的具體用法?C# GC.WaitForPendingFinalizers怎麽用?C# GC.WaitForPendingFinalizers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.GC
的用法示例。
在下文中一共展示了GC.WaitForPendingFinalizers方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
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);
}
}
}
}