What is Idisposable interface and Finalize Dispose pattern?

Discussion in 'C#' started by Sagar Jaybhay, Feb 14, 2020.

  1. Sagar Jaybhay

    Sagar Jaybhay New Member

    Joined:
    Jan 28, 2019
    Messages:
    29
    Likes Received:
    17
    Trophy Points:
    3
    Gender:
    Male
    Occupation:
    Sr. Software Developer
    Location:
    Pune
    Home Page:
    https://sagarjaybhay.net
    Garbage collector only cleans managed code and if any unmanaged code it finds it doesn’t reclaim its memory so to remove unused objects we need something. In this case, at least we have a responsibility to call these kinds of which free unmanaged code objects. In C# application there is a place where we can manage these unmanaged objects and this place is a destructor.

    But in Garbage collection destructor creates some problem which we solve by using an IDisposable Dispose pattern.


    When I write destructor then more and more objects are created in Gen1 and Gen2 so which is not good in performance point of view


    Object creation On The Heap.png


    In the above image, most of the objects are created in gen2. And code for this is below



    Code:
    
    public partial class Form1: Form
    
        {
    
            public Form1()
    
            {
    
                InitializeComponent();
    
            }
    
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                for (int i = 0; i < 1000000; i++)
    
                {
    
                    XYZ x = new XYZ();
    
                }
    
    
                MessageBox.Show("Completed");
    
            }
    
        }
    
    
        class XYZ
    
        {
    
            public int i { get; set; }
    
            public string str { get; set; }
    
    
            public void append()
    
            {
    
                for (int i = 0; i < 1000; i++)
    
                {
    
                    str += i;
    
                }
    
            }
    
    
            ~XYZ() { }
    
    
        }
    
    
    
    
    
    In below class, we implement IDisposable pattern



    Code:
    public partial class Form1: Form
    
        {
    
            public Form1()
    
            {
    
                InitializeComponent();
    
            }
    
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                for (int i = 0; i < 0000001; i++)
    
                {
    
                    XYZ x = new XYZ();
    
                    x.Dispose();
    
                }
    
    
                MessageBox.Show("Completed");
    
            }
    
        }
    
    
        class XYZ: IDisposable
    
        {
    
            public int i { get; set; }
    
            public string str { get; set; }
    
    
            public void append()
    
            {
    
                for (int i = 0; i < 1000; i++)
    
                {
    
                    str += i;
    
                }
    
            }
    
    
            ~XYZ() {
    
                //release unmanaged objects
    
                Dispose(false);
    
            }
    
    
            private void ReleaseUnmanagedResources()
    
            {
    
                // TODO release unmanaged resources here
    
            }
    
    
            protected virtual void Dispose(bool disposing)
    
            {
    
                ReleaseUnmanagedResources();
    
                if (disposing)
    
                {
    
                }
    
            }
    
    
            public void Dispose()
    
            {
    
                Dispose(true);
    
                GC.SuppressFinalize(this);
    
            }
    
        }
    
    
    
    
    
    
    GC.SuppressFinalize(this);

    This will suppress any kind of destructor and clean object.
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice