none
Is "Dispose()" method only called manually instead of Automatically? RRS feed

  • Question

  • Hi every kind member here——

    Just a very very simple question——Any instance that implemented the interface of "IDisposible" will have a method called "Dispose()", and this can be also overrloaded with a boolean value as its parameter……But what I want to ask is——

    Since this can be used in "using……" statement, so is "Dispose()" method will be automatically called REALLY?AS far as I see, only Finalize will be called automtically by GarbageCollection instead of Dispose()……

    Anyone feedback about Dispose() and Finialize?I know that Finalize will be used to deal with unmanaged things and it will be "twice" called by GC and release these memory, but what about Dispose()?Will it called by GC?If so——Not every class instance has the Dispose method,so how they are being collected and disposed without "Dispose"?

    Thxxxxxxxxx

    Monday, July 16, 2012 6:57 AM

Answers

  • Hi Ignoredrei,

    Welcome to the MSDN Forum.

    >>1)Will "Dispose" be called just like Finialize?

    No, generally, we call dispose method, and GC calls Finalize method.

    >>2) Since "Finialize" is used to Garbage to collection useless memory, why "Dispose"?

    This is why: http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx

    In some cases, you might want to provide programmers using an object with the ability to explicitly release these external resources before the garbage collector frees the object. If an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used. To provide explicit control, implement the Dispose provided by the IDisposable.

    >>3)Is "Dispose" called by Garbage to mark some object instance "useless"?if so, any other classes that WON'T implement the interface of "IDisposible" WON'T be garbaged?!!!

    As the same with 1, we call Dispose. 

    If an object didn't implement IDispose, GC will collect it when it becomes "useless". And GC will check an object is alive or not. This is for managed resource. For unmanaged resource, you have to collect it by its user, if not, memory leak occurs.

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 17, 2012 10:17 AM
    Moderator

All replies

  • hope this link can answer your question

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, July 16, 2012 7:22 AM
  • Hi,

    in Dispose method you should call GC.SupressFinalize. In documentation you find great example how to implement IDisposable interface properly.

    Monday, July 16, 2012 7:26 PM
  • Thanks everyone first——

    maybe I should make myself more clear——

    What I want to ask is——

    1)Will "Dispose" be called just like Finialize?

    2) Since "Finialize" is used to Garbage to collection useless memory, why "Dispose"?

    3)Is "Dispose" called by Garbage to mark some object instance "useless"?if so, any other classes that WON'T implement the interface of "IDisposible" WON'T be garbaged?!!!

    Tuesday, July 17, 2012 3:02 AM
  • Hi Ignoredrei,

    Welcome to the MSDN Forum.

    >>1)Will "Dispose" be called just like Finialize?

    No, generally, we call dispose method, and GC calls Finalize method.

    >>2) Since "Finialize" is used to Garbage to collection useless memory, why "Dispose"?

    This is why: http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx

    In some cases, you might want to provide programmers using an object with the ability to explicitly release these external resources before the garbage collector frees the object. If an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used. To provide explicit control, implement the Dispose provided by the IDisposable.

    >>3)Is "Dispose" called by Garbage to mark some object instance "useless"?if so, any other classes that WON'T implement the interface of "IDisposible" WON'T be garbaged?!!!

    As the same with 1, we call Dispose. 

    If an object didn't implement IDispose, GC will collect it when it becomes "useless". And GC will check an object is alive or not. This is for managed resource. For unmanaged resource, you have to collect it by its user, if not, memory leak occurs.

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 17, 2012 10:17 AM
    Moderator