Object.Finalize 메서드

정의

가비지 컬렉션이 회수하기 전에 개체가 리소스를 해제하고 다른 정리 작업을 수행할 수 있게 합니다.

!Object ()
~Object ();
abstract member Finalize : unit -> unit
override this.Finalize : unit -> unit
Finalize ()

예제

다음 예제에서는 재정의 하는 개체가 Finalize 제거 될 때 메서드가 호출 되었는지 확인 합니다 Finalize . 애플리케이션을 프로덕션에서의 Finalize 개체에서 보유 하는 관리 되지 않는 리소스를 해제 메서드를 재정의 해야 합니다. 또한 C# 예제에서는 메서드를 재정의하는 대신 소멸자를 Finalize 제공합니다.

using System;
using System.Diagnostics;

public class ExampleClass
{
   Stopwatch sw;

   public ExampleClass()
   {
      sw = Stopwatch.StartNew();
      Console.WriteLine("Instantiated object");
   }

   public void ShowDuration()
   {
      Console.WriteLine("This instance of {0} has been in existence for {1}",
                        this, sw.Elapsed);
   }

   ~ExampleClass()
   {
      Console.WriteLine("Finalizing object");
      sw.Stop();
      Console.WriteLine("This instance of {0} has been in existence for {1}",
                        this, sw.Elapsed);
   }
}

public class Demo
{
   public static void Main()
   {
      ExampleClass ex = new ExampleClass();
      ex.ShowDuration();
   }
}
// The example displays output like the following:
//    Instantiated object
//    This instance of ExampleClass has been in existence for 00:00:00.0011060
//    Finalizing object
//    This instance of ExampleClass has been in existence for 00:00:00.0036294
open System.Diagnostics

type ExampleClass() =
    let sw = Stopwatch.StartNew()
    do 
        printfn "Instantiated object"

    member this.ShowDuration() =
        printfn $"This instance of {this} has been in existence for {sw.Elapsed}"

    override this.Finalize() =
        printfn "Finalizing object"
        sw.Stop()
        printfn $"This instance of {this} has been in existence for {sw.Elapsed}"

let ex = ExampleClass()
ex.ShowDuration()
// The example displays output like the following:
//    Instantiated object
//    This instance of ExampleClass has been in existence for 00:00:00.0011060
//    Finalizing object
//    This instance of ExampleClass has been in existence for 00:00:00.0036294
Imports System.Diagnostics

Public Class ExampleClass
   Dim sw As StopWatch
   
   Public Sub New()
      sw = Stopwatch.StartNew()
      Console.WriteLine("Instantiated object")
   End Sub 

   Public Sub ShowDuration()
      Console.WriteLine("This instance of {0} has been in existence for {1}",
                        Me, sw.Elapsed)
   End Sub
   
   Protected Overrides Sub Finalize()
      Console.WriteLine("Finalizing object")
      sw.Stop()
      Console.WriteLine("This instance of {0} has been in existence for {1}",
                        Me, sw.Elapsed)
   End Sub
End Class

Module Demo
   Public Sub Main()
      Dim ex As New ExampleClass()
      ex.ShowDuration()
   End Sub
End Module
' The example displays output like the following:
'    Instantiated object
'    This instance of ExampleClass has been in existence for 00:00:00.0011060
'    Finalizing object
'    This instance of ExampleClass has been in existence for 00:00:00.0036294

메서드를 재정의하는 추가 예제는 Finalize 메서드를 참조하세요 GC.SuppressFinalize .

설명

이 API에 대한 자세한 내용은 Object.Finalize에 대한 추가 API 설명을 참조하세요.

적용 대상

추가 정보