views:

119

answers:

7

I am not sure how the user defined class objects are garbage collected. Do I need to implement IDisposable interface on every class and call the dispose() method on it to free the memory?

+3  A: 

No, every object is disposed automatically.

IDisposable is usable when you have some kind of special operation to do at the object deconstruction. For example if you locks some resources like files or such during the work of your object, then you can release them in your Dispose method.

ŁukaszW.pl
+1  A: 

IDisposable must be used when you want to use some native resource like file stream in your code. For all other purposes, the .Net garbage collector does a good job.

Simply implmenting the IDisposable does not force a garbage collection. The Dispose method needs to be called explicitly to clean up any native resource.

Edit: I will advise you to go through the Garbage Collection chapter in ClR via C# by Jeff Richter.

Yogendra
+1  A: 

No. Your user defined class is garbaged collection when it is not referenced anywhere anymore. You can force this by just doing classObject = null. If that is the last part of the application that is currently referencing classObject then the garbage collection will clear its memory.

IDisposable is mostly used so you can organize the garbage collection activities via the using() methods.

KallDrexx
+7  A: 

No. Every normal managed .NET object gets garbage collected when you stop referring to it. IDisposable means that you you will implement a Dispose() method that needs to be called by the caller -- it usually releases things that aren't garbaged collected. It also helps with having a deterministic place to release memory.

Check out the IDisposable pattern to make sure you do it right:

http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/09/21/10887.aspx

Lou Franco
Thanks for your response. So if I have a class A and one of its public function creates unmanaged object like Bitmap and calls BitMap.Dispose(), then will it be garbage collected or does the class A keeps a reference to the bitmap?
kishore
When you call Dispose(), it will be disposed of right then (the object is now unusable). If you didn't, the GC would get to it eventually, and if it was written right (it was) then the Finalize will call Dispose(). But, calling Dispose() is required (use the using statement to do it automatically).
Lou Franco
Also, you should definitely not implement IDisposable unless you need to. It makes the GC slower to have to deal with them (remembering to call Finalize, etc). If you have a property that's disposable or unmanaged resources, then go implement it.
Lou Franco
+1  A: 

No it does not. The Dispose() method is designed to do extra work do dispose resources which have to be disposed when the object isn't used anymore. Remember, garbage collected can happen a long time after the object lost it last reference.

When the IDisposable is used you should dispose the object when your done with fx using like

using (SomeClassWithDisp object = new SomeClassWithDisp())
{
    //Use the object
}

Also, see:

http://stackoverflow.com/questions/339063/what-is-the-difference-between-using-idisposable-vs-a-destructor-in-c

lasseespeholt
+1  A: 

In .NET, all objects should be released from the memory with the garbage collector by default as long as they're not being used anymore, so you don't necessarely need to implement IDisposable on every class, but it's good to have some sort of memory release mechanism, like a destructor or the Dispose method itself.

This is a good example of a destructor being used: http://www.developer.com/net/csharp/article.php/3343191/C-Tip-Forcing-Garbage-Collection-in-NET.htm

Thiago Santos
+1  A: 

The dispose() method you implement is used to explicitly free up unmanaged resources (files, streams, handles, etc) to which your object holds a reference.

The idea is that you will free up these resources as soon as possible, by calling the dispose method. The dispose() method will not IMMEDIATELY run garbage collection on your object, but rather release the resources and allow the garbage collector to do its thing when convenient for it.

baultista