Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDisposable, does it really matter

Coming from C/C++ a long time ago I still have a habit of ensuring that all resources are cleaned up correctly. I always ensure Dispose is called on IDisposable classes and implement Dispose patterns in my classes containing disposable objects.

However, in my environment I'm more or less the only one doing this. Others just don't understand what I'm doing and think my code is more difficult to understand.

They just create database connections, open streams etc without calling Close or Dispose. Sometimes they set a local or member variable to "Nothing" at the end of a method (guess their background).

My problem is that their code works just as well as mine. Code that over time creates thousands of database connection objects just works.

So, ignoring any arguments about code correctness, following guidelines etc, does IDiposable really matter?

Has anyone actually ran out of resources from not Disposing objects?

Edit: Thanks for all replies. Interesting to see that some people have had problems when not Disposing. It seems to be rare though and I assume the GC/JIT does a good job of keeping resource usage down under normal conditions.

Neither my collegues nor I will change behavior because of this but it feels good to be right.

like image 470
adrianm Avatar asked Mar 17 '10 19:03

adrianm


People also ask

Why do we need IDisposable?

You never know when the garbage collector will collect your object. You don't even know if it even will (unlike using delete in C++, which is deterministic). So IDisposable is there for deterministically releasing unneeded references (and releasing unmanaged resources).

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.

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.

What does IDisposable mean?

IDisposable is a standard interface in the . NET framework that facilitates the deterministic release of unmanaged resources.


2 Answers

Yes, I've maxed out the number of Oracle cursors when looping over a connection object, for example, because I forgot to close the command reader - that was only 100 loops on a single connection too, and I needed to support that with possibly hundreds of connections doing it at the same time.

Your fellow developers should be taught to use the using() { ... } syntax if they can't be bothered to close up any unmanaged resources themselves. It is good practice anyways and you should use it too, since you yourself might be forgetting to put your Dispose() calls in a finally {} clause so as to truly clean up in the event of an unhandled exception being thrown.

If you can't win their hearts - change their minds - create tests that break their code by maxing out the resources that they're not cleaning up - and then show that the "fix" is simple and easy, and enables their code to be far more scalable. Or just show it to your boss and tell them this will enable him/her to sell the product as a new version with more scalability built in :) Your fellow developers will be instructed to do this all the time in the future, hopefully, and you'll be held in higher regard too.

like image 96
Mike Atlas Avatar answered Oct 27 '22 06:10

Mike Atlas


Yes it matters. When an object implements IDisposable it is explicitly stating that it is holding resources that need to be released when the object is no longer needed.

Most will still clean up their resources when the object is finalized but finalization is not deterministic and can't be relied on for resource management.

Simply wrapping the variable declarations in a using(...) block makes it easy to dispose of properly.

like image 37
Paul Alexander Avatar answered Oct 27 '22 06:10

Paul Alexander