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?
views:
119answers:
7Does every user defined class needs to implement IDisposable interface to get garbage collected
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.
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.
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.
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
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:
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
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.