32

I know C# can manage resource pretty well with its garbage collector. But since it has that, what exactly is this for and why is it needed?

Can anyone explain why .Dispose() is needed in asp.net mvc?

Also, what does it mean to Dispose a connection? Why is it needed? Anyone know the intricacies of why it's important to dispose a database connection like in db.Dispose()? Is this EF-related, or SQL Server-related? I'm trying to understand exactly why.

protected override void Dispose(bool disposing)
{
   db.Dispose();
   base.Dispose(disposing);
}
Jan Carlo Viray
  • 11,856
  • 11
  • 42
  • 63
  • 2
    Generally, if it's an `IDisposable`, and you're holding on to anything that's an `IDisposable`, provide a `Dispose` method. And use it. – cHao Apr 13 '12 at 02:12
  • I've heard of that ...but exactly why though? Doesn't C# have a garbage collector that destroys unneeded objects once they go out of scope? So, why is there an explicit .Dispose() method such as in Controllers for asp.net mvc? And even on Entity Framework? What exactly does it do? – Jan Carlo Viray Apr 13 '12 at 02:14
  • possible duplicate of [C# Finalize/Dispose pattern](http://stackoverflow.com/questions/898828/c-sharp-finalize-dispose-pattern) Please check out top answers for IDisposable tag (there is nothing specific to MVC in usage and implementation of IDisposable). – Alexei Levenkov Apr 13 '12 at 02:15
  • @JanCarloViray - It allows you to control when your unmanaged resources get disposed (such as those that aren't garbage collected). – M.Babcock Apr 13 '12 at 02:16
  • 3
    @AlexeiLevenkov: That's not an exact duplicate. Closing specific questions as duplicates of a more general, tutorial-style question is the moral equivalent of saying "RTFM." See http://meta.stackexchange.com/a/110002/102937. The OP is asking specifically about the Dispose() method's significance and use in MVC controllers. In other words, what is the specific reason you need the Dispose pattern there? What exactly is it disposing? – Robert Harvey Apr 13 '12 at 02:31

1 Answers1

31

Dispose is for releasing "unmanaged" resources (for example, sockets, file handles, Bitmap handles, etc), and if it's being called outside a finalizer (that's what the disposing flag signifies, BTW), for disposing other IDisposable objects it holds that are no longer useful.

"Unmanaged" resources aren't managed by the CLR (hence the name), and GC doesn't mess with them or free them all by itself; absent a Dispose method (and code actually using it!), it'll rely on the object's finalizer to clean up. Eventually the finalizer will run (if the app's healthy, and the object has a finalizer), and if the finalizer does its job then all's semi OK....but it'll take its sweet time in doing so -- and if you run out of handles in the meantime, oh well. Too bad for that other thread/process/whatever that needed them.

If you Dispose, though, the resources are released immediately, and things run better all around.

(By the way, this is not restricted to EF, SQL Server, or any other technology. The Disposable pattern is found throughout the .net framework, and it's considered good practice to take advantage of it whenever you have an IDisposable that's no longer being used.)

As for why IDisposable is implemented so far up the tree, rather than you just implementing it on a case by case basis...i'm not 100% sure. But imagine you were writing a framework. Consider that if everything weren't an IDisposable, you'd have to check -- every time you wanted to get rid of something! -- whether the object is disposable, and Dispose it if so. If you instead implement IDisposable "just in case", though, things are simplified -- you just always dispose. (If an object doesn't have anything to clean up, it just doesn't override Dispose -- in which case its parent's Dispose gets called and does whatever cleanup it has to, which may be nothing as well...) And it's a common enough case for controllers to have stuff to clean up, that even if that's not the real reason, it makes a lot of sense to do anyway.

cHao
  • 84,970
  • 20
  • 145
  • 172
  • 3
    Which unmanaged resources are released by ASP.NET MVC controllers? – Robert Harvey Apr 13 '12 at 02:36
  • 1
    Depends on what they do, and how they do it. I don't know the innards of ASP.net MVC, but i imagine there's plenty of file handles involved. Controllers, possibly other objects containing a DB connection? Don't know, don't care. If you're holding on to anything that's an IDisposable, it's good manners to provide a `Dispose` method...and for whatever's holding your object to call it. – cHao Apr 13 '12 at 02:40
  • So why dispose is cleaning my TempData? – ErTR Apr 22 '20 at 02:11
  • @ErTR: Like i said, i don't know about the innards of ASP.net MVC. But this sounds like a new question, and largely unrelated to `Controller.Dispose(bool)` (which apparently doesn't even do anything by default; it's there entirely to be overridden by more useful controllers). It sounds like you might be trying to use TempData for longer-lived data than it was designed for. – cHao Apr 22 '20 at 13:59