Differentiate between Object Finalizers and Disposable Objects.

Finalizers

Finalizers are special methods that are automatically called by the GC before the object is collected. They can only be called by the GC provided they exist. The .NET ultimate base class Object has a Finalize method that can be overridden by child objects (anyone basically). The purpose of finalizers is to ensure all unmanaged resources the object may be using are properly cleaned up prior to the end of the object lifetime.
If a type has an implemented (overridden) finalizer at the time of collection, the GC will first put the object in the finalization queue, then call the finalizer and then the object is destroyed.Finalizers are not directly supported by C# compilers; instead you should use destructors using the ~ character, like so:

public class myclass
    {
        ~myclass()
        {
            Console.WriteLine("destructor execute");
            //cleanup unmanaged resourse here
        }
    }

The CLR implicitly translates C# destructors to create Finalize calls.

 public override void Finalize()
{
    try
{
    console.writeline("destructor execute");
    //cleanup unmanaged resourse here
}
    finally
{
    base.Finalize();
}
}

Facts about Finalizers

  • Finalizers execution during garbage collection is not guaranteed at any specific time, unless calling a Close or a Dispose method.
  • Finalizers of 2 different objects are not guaranteed to run in any specific order, even if they are part of object composition.
  • Finalizers (or destructors) should ONLY be implemented when your object type directly handles unmanaged resources. Only unmanaged resources should be freed up inside the finalizer.
  • Finalizers must ALWAYS call the base.Finalize() method of the parent (this is not true for C# destructors, that do this automatically for you)
  • C# does not support finalizers directly, but it supports them through C# destructors.
  • Finalizers run in arbitrary threads selected or created by the GC.
  • The CLR will only continue to Finalize objects in the finalization queue <=> the number of finalizable objects in the queue continues to decrease.
  • All finalizer calls might not run to completion if one of the finalizers blocks indefinitely (in the code) or the process in which the app domain is running terminates without giving the CLR chance to clean up.

Disposable Objects

Objects in .NET can implement the IDisposable interface, whose only contract is to implement the Dispose method. Disposable objects are only created explicitly and called explicitly by the developer, and its main goal is to dispose managed and unmanaged resources on demand (when the developer wants to). The GC never calls the Dispose method on an object automatically. The Dispose() method on a disposable object can only get executed in one  two scenarios:

  • The developer invokes a direct call to dispose the object.
  • The developer created the object in the context of the using statement
Given the following disposable object type:


public  class Myclass : IDisposable
{
    public void dispose()
    {
        //cleanup resources here
    }
}

The next alternatives are equivalent:

public void Mymethod()
    {
        Myclass myClass = null;
        try{
           myClass =new myClass();
            Console.WriteLine("using Myclass instance{0},myClass");
        //use myClass instance
    }
        finally{
            if(myClass!=null)
                myClass.Dispose();
        }
}

public void Mymethod1()
    {
       using(var myClass=new Myclass())
       {
           Console.WriteLine("using Myclass instance{0},myClass");
        //use myclass instance
    }
       
}

Facts about Disposable Objects

  • An object is a disposable object if implements the IDisposable interface.
  • The only method of the IDisposable interface is the Dispose() method.
  • On the Dispose method the developer can free up both managed and unmanaged resources.
  • Disposable objects can be used by calling directly the object.Dispose() method or using the object within a using statement
  • The Dispose() method on disposable objects will never get called automatically by the GC.

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*