ファイナライザのキューを処理するスレッドがそのキューを空にするまで、現在のスレッドを中断します。
Public Shared Sub WaitForPendingFinalizers()
[C#]
public static void WaitForPendingFinalizers();
[C++]
public: static void WaitForPendingFinalizers();
[JScript]
public static function WaitForPendingFinalizers();
解説
ガベージ コレクタは、収集できるオブジェクトを見つけると、それぞれのオブジェクトを検査して、終了操作が必要かどうかを判断します。オブジェクトがファイナライザを実装していて、終了操作を無効にする SuppressFinalize を呼び出していない場合、そのオブジェクトは終了処理の準備ができたものとしてマークされたオブジェクトのリストに入れられます。ガベージ コレクタは、このリストにあるオブジェクトの Finalize メソッドを呼び出し、そのエントリをリストから削除します。このメソッドは、すべてのファイナライザが終了するまでブロックします。
ファイナライザを実行するスレッドは指定されないため、このメソッドが終了する保証はありません。ただし、 WaitForPendingFinalizers メソッドが実行中の場合、別のスレッドはこのスレッドを中断することができます。たとえば、ある期間だけ待機する別のスレッドを起動した後、このスレッドがまだ中断されていれば、このスレッドに割り込むことができます。
使用例
Imports System
Namespace WaitForPendingFinalizersExample
Class MyWaitForPendingFinalizersClass
' You can increase this number to fill up more memory.
Private Const numMfos As Integer = 1000
' You can increase this number to cause more
' post-finalization work to be done.
Private Const maxIterations As Integer = 100
Overloads Shared Sub Main()
Dim mfo As MyFinalizeObject = Nothing
' Create and release a large number of objects
' that require finalization.
Dim j As Integer
For j = 0 To numMfos - 1
mfo = New MyFinalizeObject()
Next j
'Release the last object created in the loop.
mfo = Nothing
'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.
Dim i As Integer
For i = 0 To maxIterations - 1
Console.WriteLine("Doing some post-finalize work")
Next i
End Sub
End Class
Class MyFinalizeObject
' Make this number very large to cause the finalizer to
' do more work.
Private maxIterations As Integer = 10000
Protected Overrides Sub Finalize()
Console.WriteLine("Finalizing a MyFinalizeObject")
' Do some work.
Dim i As Integer
For i = 0 To maxIterations - 1
' This method performs no operation on i, but prevents
' the JIT compiler from optimizing away the code inside
' the loop.
GC.KeepAlive(i)
Next i
MyBase.Finalize()
End Sub
End Class
End Namespace
[C#]
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);
}
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
__gc class MyFinalizeObject {
// Make this number very large to cause the finalizer to
// do more work.
private:
static const int maxIterations = 10000;
~MyFinalizeObject() {
Console::WriteLine(S"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( __box(i));
}
}
};
// 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;
int main() {
MyFinalizeObject* mfo = 0;
// 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 = 0;
//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(S"Doing some post-finalize work");
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
GC クラス | GC メンバ | System 名前空間