none
Difference between Dispose and Finalize methods in .Net RRS feed

  • Question

  • Hi all, i would like to know what is difference between Dispose, Finalize and Destructor. when to use Finalize and Dispose methods.

    Thanks in Advance

     
    Thursday, September 26, 2013 2:35 AM

Answers

  • Hi Gnanasekaran,

    The important difference is timing.  Finalize is called after the .NET garbage collector runs.  That can take a while, depending on how frequently you allocate memory and in what generation the object lives.  Dispose() is the standardized way to free up the unmanaged resources your object uses well before the finalizer gets a chance to do so.  A Bitmap would be a good example, it takes very few managed resources but can eat a huge chunk of unmanaged memory, depending on the size of the bitmap.  If you don't call Dispose on it after you're done using it, your app can potentially consume large amounts of unused memory for an extended amount of time.

    Normally, we consider Finalize deals with managed resources and Dispose deals with unmanaged resources. GC will invoke Finalize automatically and we don’t need to take care of it. If a class (such as FileStream, TcpClient, NetWorkStream, SqlConnection and so on) has unmanaged resources you need to invoke Dispose (normally named as close) timely after using it.

    You could read more about it in below reference.

    Object.Finalize Method

    IDisposable.Dispose Method

    Destructors

    Best Regards,

    Hetro


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 27, 2013 12:40 AM
    Moderator

All replies

  • Hi Gnanasekaran,

    The important difference is timing.  Finalize is called after the .NET garbage collector runs.  That can take a while, depending on how frequently you allocate memory and in what generation the object lives.  Dispose() is the standardized way to free up the unmanaged resources your object uses well before the finalizer gets a chance to do so.  A Bitmap would be a good example, it takes very few managed resources but can eat a huge chunk of unmanaged memory, depending on the size of the bitmap.  If you don't call Dispose on it after you're done using it, your app can potentially consume large amounts of unused memory for an extended amount of time.

    Normally, we consider Finalize deals with managed resources and Dispose deals with unmanaged resources. GC will invoke Finalize automatically and we don’t need to take care of it. If a class (such as FileStream, TcpClient, NetWorkStream, SqlConnection and so on) has unmanaged resources you need to invoke Dispose (normally named as close) timely after using it.

    You could read more about it in below reference.

    Object.Finalize Method

    IDisposable.Dispose Method

    Destructors

    Best Regards,

    Hetro


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 27, 2013 12:40 AM
    Moderator
  • Thanks Hetro, i could easily understand the difference between Finalize and Dispose methods. 
    Friday, September 27, 2013 12:41 PM