GC.WaitForPendingFinalizers Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menangguhkan utas saat ini hingga utas yang memproses antrean finalizer telah mengkosongkan antrean tersebut.
public:
static void WaitForPendingFinalizers();
public static void WaitForPendingFinalizers();
static member WaitForPendingFinalizers : unit -> unit
Public Shared Sub WaitForPendingFinalizers ()
Contoh
Contoh berikut menunjukkan cara menggunakan WaitForPendingFinalizers metode untuk menangguhkan utas saat ini hingga finalisasi semua objek yang dikumpulkan selesai.
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
Keterangan
Ketika pengumpul sampah menemukan objek yang dapat direklamasi kembali, ia memeriksa setiap objek untuk menentukan persyaratan finalisasi objek. Jika objek mengimplementasikan finalizer dan belum menonaktifkan finalisasi dengan memanggil SuppressFinalize, objek ditempatkan dalam daftar objek yang ditandai sebagai siap untuk finalisasi. Pengumpul sampah memanggil Finalize metode untuk objek dalam daftar ini dan menghapus entri dari daftar. Metode ini memblokir hingga semua finalizer berjalan hingga selesai.
Utas tempat finalizer dijalankan tidak ditentukan, sehingga tidak ada jaminan bahwa metode ini akan dihentikan. Namun, utas ini dapat terganggu oleh utas lain saat WaitForPendingFinalizers metode sedang berlangsung. Misalnya, Anda dapat memulai utas lain yang menunggu untuk jangka waktu tertentu dan kemudian mengganggu utas ini jika utas ini masih ditangguhkan.