Search code examples
c#objectgarbage-collectiondisposefinalizer

Garbage collection, and dispose questions. Appreciate some brainy assistance


Does the Dispose method use Garbage collector to clear resources?
Also is there any benefit in using Dispose instead of finalizers?
I have searched for answeres but nothing very good at explaining it so far.
Thank you.


Solution

  • Does the dispose () method use Garbage collection to clear resources?

    No. Dispose() is used for releasing unmanaged resources (usually). It doesn't have anything to do with GC.

    See: IDisposable interface

    The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

    Garbage collector calls the finalizer (destructor) of an object and if that object implements IDisposable then the finalizer can have a call to Dispose(false)