GC.ReRegisterForFinalize メソッド
アセンブリ: mscorlib (mscorlib.dll 内)
構文
例外
解説
ReRegisterForFinalize メソッドは、ガベージ コレクタがオブジェクトを解放する前に、終了操作を要求するオブジェクトのリストに obj を追加します。obj パラメータには、このメソッドの呼び出し元を指定する必要があります。
ReRegisterForFinalize メソッドを呼び出しても、ガベージ コレクタがオブジェクトのファイナライザを呼び出すという保証はありません。
既定では、ファイナライザを実装するすべてのオブジェクトは、終了操作を要求をするオブジェクトのリストに追加されます。ただし、オブジェクトが既に終了されている場合や、SuppressFinalize メソッドを呼び出すことにより終了操作が無効にされている場合などがあります。
Imports System Namespace ReRegisterForFinalizeExample Class MyMainClass Shared Sub Main() 'Create a MyFinalizeObject. Dim mfo As New MyFinalizeObject() 'Release the reference to mfo. mfo = Nothing 'Force a garbage collection. GC.Collect() 'At this point mfo will have gone through the first Finalize. 'There should now be a reference to mfo in the static 'MyFinalizeObject.currentInstance field. Setting this value 'to null and forcing another garbage collection will now 'cause the object to Finalize permanently. MyFinalizeObject.currentInstance = Nothing GC.Collect() End Sub End Class Class MyFinalizeObject Public Shared currentInstance As MyFinalizeObject = Nothing Private hasFinalized As Boolean = False Protected Overrides Sub Finalize() If hasFinalized = False Then Console.WriteLine("First finalization") 'Put this object back into a root by creating 'a reference to it. MyFinalizeObject.currentInstance = Me 'Indicate that this instance has finalized once. hasFinalized = True 'Place a reference to this object back in the 'finalization queue. GC.ReRegisterForFinalize(Me) Else Console.WriteLine("Second finalization") End If MyBase.Finalize() End Sub End Class End Namespace
using System; namespace ReRegisterForFinalizeExample { class MyMainClass { static void Main() { // Create a MyFinalizeObject. MyFinalizeObject mfo = new MyFinalizeObject(); // Release the reference to mfo. mfo = null; // Force a garbage collection. GC.Collect(); // At this point mfo will have gone through the first Finalize. // There should now be a reference to mfo in the static // MyFinalizeObject.currentInstance field. Setting this value // to null and forcing another garbage collection will now // cause the object to Finalize permanently. MyFinalizeObject.currentInstance = null; GC.Collect(); } } class MyFinalizeObject { public static MyFinalizeObject currentInstance = null; private bool hasFinalized = false; ~MyFinalizeObject() { if(hasFinalized == false) { Console.WriteLine("First finalization"); // Put this object back into a root by creating // a reference to it. MyFinalizeObject.currentInstance = this; // Indicate that this instance has finalized once. hasFinalized = true; // Place a reference to this object back in the // finalization queue. GC.ReRegisterForFinalize(this); } else { Console.WriteLine("Second finalization"); } } } }
using namespace System; ref class MyFinalizeObject { public: static MyFinalizeObject^ currentInstance = nullptr; private: bool hasFinalized; public: MyFinalizeObject() { hasFinalized = false; } ~MyFinalizeObject() { if ( hasFinalized == false ) { Console::WriteLine( "First finalization" ); // Put this object back into a root by creating // a reference to it. MyFinalizeObject::currentInstance = this; // Indicate that this instance has finalized once. hasFinalized = true; // Place a reference to this object back in the // finalization queue. GC::ReRegisterForFinalize( this ); } else { Console::WriteLine( "Second finalization" ); } } }; int main() { // Create a MyFinalizeObject. MyFinalizeObject^ mfo = gcnew MyFinalizeObject; // Release the reference to mfo. mfo = nullptr; // Force a garbage collection. GC::Collect(); // At this point mfo will have gone through the first Finalize. // There should now be a reference to mfo in the static // MyFinalizeObject::currentInstance field. Setting this value // to 0 and forcing another garbage collection will now // cause the object to Finalize permanently. MyFinalizeObject::currentInstance = nullptr; GC::Collect(); }
package ReRegisterForFinalizeExample; import System.* ; class MyMainClass { public static void main(String[] args) { // Create a MyFinalizeObject. MyFinalizeObject mfo = new MyFinalizeObject(); // Release the reference to mfo. mfo = null; // Force a garbage collection. GC.Collect(); // At this point mfo will have gone through the first Finalize. // There should now be a reference to mfo in the static // MyFinalizeObject.currentInstance field. Setting this value // to null and forcing another garbage collection will now // cause the object to Finalize permanently. MyFinalizeObject.currentInstance = null; GC.Collect(); } //main } //MyMainClass class MyFinalizeObject { public static MyFinalizeObject currentInstance = null; private boolean hasFinalized = false; public void finalize() { if (hasFinalized == false) { Console.WriteLine("First finalization"); // Put this object back into a root by creating // a reference to it. MyFinalizeObject.currentInstance = this; // Indicate that this instance has finalized once. hasFinalized = true; // Place a reference to this object back in the // finalization queue. GC.ReRegisterForFinalize(this); } else { Console.WriteLine("Second finalization"); } 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- GC.ReRegisterForFinalize メソッドのページへのリンク