Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is IDisposable for?

Tags:

If .NET has garbage collection then why do you have to explicitly call IDisposable?

like image 483
FendFend Avatar asked Mar 05 '09 14:03

FendFend


People also ask

Why would you need the IDisposable?

IDisposable. Dispose method. The consumer of an object should call this method when the object is no longer needed. The IDisposable interface is generally provided for the release of unmanaged resources that need to be reclaimed in some order or time dependent manner.

When should you use IDisposable?

If you access unmanaged resources (e.g. files, database connections etc.) in a class, you should implement IDisposable and overwrite the Dispose method to allow you to control when the memory is freed.

What happens if you dont Dispose IDisposable?

IDisposable is usually used when a class has some expensive or unmanaged resources allocated which need to be released after their usage. Not disposing an object can lead to memory leaks.

Is IDisposable called automatically?

By default, the garbage collector automatically calls an object's finalizer before reclaiming its memory. However, if the Dispose method has been called, it is typically unnecessary for the garbage collector to call the disposed object's finalizer.


2 Answers

Garbage collection is for memory. You need to dispose of non-memory resources - file handles, sockets, GDI+ handles, database connections etc. That's typically what underlies an IDisposable type, although the actual handle can be quite a long way down a chain of references. For example, you might Dispose an XmlWriter which disposes a StreamWriter it has a reference to, which disposes the FileStream it has a reference to, which releases the file handle itself.

like image 179
Jon Skeet Avatar answered Oct 05 '22 08:10

Jon Skeet


Expanding a bit on other comments:

The Dispose() method should be called on all objects that have references to un-managed resources. Examples of such would include file streams, database connections etc. A basic rule that works most of the time is: "if the .NET object implements IDisposable then you should call Dispose() when you are done with the object.

However, some other things to keep in mind:

  • Calling dispose does not give you control over when the object is actually destroyed and memory released. GC handles that for us and does it better than we can.
  • Dispose cleans up all native resources, all the way down the stack of base classes as Jon indicated. Then it calls SuppressFinalize() to indicate that the object is ready to be reclaimed and no further work is needed. The next run of the GC will clean it up.
  • If Dispose is not called, then GC finds the object as needing to be cleaned up, but Finalize must be called first, to make sure resources are released, that request for Finalize is queued up and the GC moves on, so the lack of a call to Dispose forces one more GC to run before the object can be cleaned. This causes the object to be promoted to the next "generation" of GC. This may not seem like a big deal, but in a memory pressured application, promoting objects up to higher generations of GC can push a high-memory application over the wall to being an out-of-memory application.
  • Do not implement IDisposable in your own objects unless you absolutely need to. Poorly implemented or unneccessary implementations can actually make things worse instead of better. Some good guidance can be found here:

    Implementing a Dispose Method

    Or read that whole section of MSDN on Garbage Collection

like image 40
TheZenker Avatar answered Oct 05 '22 08:10

TheZenker