Finalize Method in C#



What is Finalize in C# ?

  • Enables an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
  • The Finalize method is used to perform cleanup operations on unmanaged resources held by the current object before the object is destroyed.
  • The method is protected and therefore is accessible only through this class or through a derived class.

Syntax

protected virtual void Finalize()

How finalization works

  • The Object class provides no implementation for the Finalize method, and the garbage collector does not mark types derived from Object for finalization unless they override the Finalize method.
  • If a type does override the Finalize method, the garbage collector adds an entry for each instance of the type to an internal structure called the finalization queue.
  • The finalization queue contains entries for all the objects in the managed heap whose finalization code must run before the garbage collector can reclaim their memory.
  • The garbage collector then calls the Finalize method automatically under the following conditions:
    • After the garbage collector has discovered that an object is inaccessible, unless the object has been exempted from finalization by a call to the GC.SuppressFinalize method.
    • During shutdown of an application domain, unless the object is exempt from finalization. During shutdown, even objects that are still accessible are finalized.
    • Finalize is automatically called only once on a given instance, unless the object is re-registered by using a mechanism such as GC.ReRegisterForFinalize and the GC.SuppressFinalize method has not been subsequently called.

Limitation:

  • The exact time when the finalizer executes is undefined.
  • To ensure deterministic release of resources for instances of your class, implement a Close method or provide a IDisposable.Dispose implementation.
  • The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other.
  • That is, if Object A has a reference to Object B and both have finalizers, Object B might have already been finalized when the finalizer of Object A starts.
  • The thread on which the finalizer runs is unspecified.
  • The finalizer implicitly calls Finalize on the base class of the object. Therefore, a call to a finalizer is implicitly translated to the following code:
protected override void Finalize()  
{  
    try  
    {  
        // Cleanup statements...  
    }  
    finally  
    {  
        base.Finalize();  
    }  
}  
  • This means that the Finalize method is called recursively for all instances in the inheritance chain, from the most-derived to the least-derived.

Sample code

class First
{
    ~First()
    {
        System.Diagnostics.Trace.WriteLine("First's destructor is called.");
    }
}

class Second : First
{
    ~Second()
    {
        System.Diagnostics.Trace.WriteLine("Second's destructor is called.");
    }
}

class Third : Second
{
    ~Third()
    {
        System.Diagnostics.Trace.WriteLine("Third's destructor is called.");
    }
}

class TestDestructors
{
    static void Main()
    {
        Third t = new Third();
    }

}

Output

Third's destructor is called.
    Second's destructor is called.
    First's destructor is called.

Related Searches to Finalize Method in C#