Search code examples
c#disposeunmanaged

dispose vs finalize how the object free the memory?


I want to know should we use dispose and finalize if we are to deal with an object that is holding an unmanaged resource. second thing if we dispose an object does that object release memory along with the unmanaged resource on that moment only or that object's memory will be released by garbage collector later. same thing i want to know in fianilize context, once we finalize an object does that release memory for that object on the definite time or we have wait until garbage collector frees it's memory.


Solution

  • You should prevent users of your application from calling an object's Finalize method directly by limiting its scope to protected. In addition, you are strongly discouraged from calling a Finalize method for a class other than your base class directly from your application's code. To properly dispose of unmanaged resources, it is recommended that you implement a public Dispose or Close method that executes the necessary cleanup code for the object. The IDisposable interface provides the Dispose method for resource classes that implement the interface. Because it is public, users of your application can call the Dispose method directly to free memory used by unmanaged resources. When you properly implement a Dispose method, the Finalize method becomes a safeguard to clean up resources in the event that the Dispose method is not called.

    // Design pattern for a base class.
    public class Base: IDisposable
    {
        private bool disposed = false;
    
        //Implement IDisposable.
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Free other state (managed objects).
                }
                // Free your own state (unmanaged objects).
                // Set large fields to null.
                disposed = true;
            }
        }
    
        // Use C# destructor syntax for finalization code.
        ~Base()
        {
            // Simply call Dispose(false).
            Dispose (false);
        }
    }
    
    // Design pattern for a derived class.
    public class Derived: Base
    {
        private bool disposed = false;
    
        protected override void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Release managed resources.
                }
                // Release unmanaged resources.
                // Set large fields to null.
               // Call Dispose on your base class.
                disposed = true;
            }
            base.Dispose(disposing);
        }
        // The derived class does not have a Finalize method
        // or a Dispose method without parameters because it inherits
        // them from the base class.
    }
    

    Source: MSND