GC.WaitForPendingFinalizers 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
종료자의 큐를 처리하는 스레드에서 해당 큐를 비울 때까지 현재 스레드를 일시 중단합니다.
public:
static void WaitForPendingFinalizers();
public static void WaitForPendingFinalizers ();
static member WaitForPendingFinalizers : unit -> unit
Public Shared Sub WaitForPendingFinalizers ()
예제
다음 예제에서는 메서드를 사용하여 WaitForPendingFinalizers 수집된 모든 개체의 완료가 완료될 때까지 현재 스레드를 일시 중단하는 방법을 보여 줍니다.
using namespace System;
ref class MyFinalizeObject
{
private:
// Make this number very large to cause the finalizer to
// do more work.
literal 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 );
}
}
};
// 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 = nullptr;
// Create and release a large number of objects
// that require finalization.
for ( int j = 0; j < numMfos; j++ )
{
mfo = gcnew MyFinalizeObject;
}
//Release the last object created in the loop.
mfo = nullptr;
//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" );
}
}
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);
}
}
}
}
open System
// You can increase this number to fill up more memory.
let numMfos = 1000
// You can increase this number to cause more
// post-finalization work to be done.
let maxIterations = 100
[<AllowNullLiteral>]
type MyFinalizeObject() =
// Make this number very large to cause the finalizer todo more work.
let maxIterations = 10000
override _.Finalize() =
printfn "Finalizing a MyFinalizeObject"
// Do some work.
for i = 1 to maxIterations do
// This method performs no operation on i, but prevents
// the JIT compiler from optimizing away the code inside
// the loop.
GC.KeepAlive i
let mutable mfo = null
// Create and release a large number of objects
// that require finalization.
for j = 1 to numMfos do
mfo <- 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 _ = 1 to maxIterations do
printfn "Doing some post-finalize work"
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
설명
가비지 수집기는 회수할 수 있는 개체를 찾으면 각 개체를 검사하여 개체의 종료 요구 사항을 확인합니다. 개체가 종료자를 구현하고 를 호출 SuppressFinalize하여 종료를 사용하지 않도록 설정하지 않은 경우 개체는 종료할 준비가 된 것으로 표시된 개체 목록에 배치됩니다. 가비지 수집기는 이 목록의 Finalize 개체에 대한 메서드를 호출하고 목록에서 항목을 제거합니다. 이 메서드는 모든 종료자가 완료될 때까지 차단합니다.
종료자가 실행되는 스레드는 지정되지 않으므로 이 메서드가 종료된다는 보장은 없습니다. 그러나 메서드가 진행 중인 동안 이 스레드는 다른 스레드에 WaitForPendingFinalizers 의해 중단될 수 있습니다. 예를 들어 일정 시간 동안 대기하는 다른 스레드를 시작한 다음, 이 스레드가 여전히 일시 중단된 경우 이 스레드를 중단할 수 있습니다.
적용 대상
.NET