Reputation: 599
Since C# uses Garbage Collection. When is it necessary to use .Dispose to free the memory?
I realize there are a few situations so I'll try to list the ones I can think of.
Upvotes: 8
Views: 10134
Reputation: 6224
Like Reed Copsey said, it's usually not necessary to call Dispose.
A possible case that may be creating your problem is that a static object is holding references of other objects that are no longer used anywhere else. The following code shows an example:
Form_Load(...)
MyState.Instance.AddressChanged += this.User_AddressChanged;
End
If for some reason, when form is unloaded, the code does not unregister the event handler, the form instance will still be referenced by the state object.
Upvotes: 1
Reputation: 72658
You should call Dispose
on every class that implements IDisposable. If it didn't need to be Dispose
ed then it wouldn't implement IDisposable
.
As for your other questions:
Controls
collection, then the control will be automatically disposed when the form is closed, so there's nothing you need to do there.IDisposable
, then you need to call Dispose
. For example, if you go new FileStream(...)
, then the FileStream needs to be disposed, since it implements IDisposable. I would suggest you read up on the using
construct in C# which makes it easier to handle IDisposable
objects.Upvotes: 3
Reputation: 1193
If you are using an IDisposable
object, consider using the using
statement to automatically deal with the disposing for you.
Upvotes: 0
Reputation: 564433
In theory, if you have properly defined componentry, it should never be required to call Dispose() on your objects, as the finalizer should eventually take care of it.
That being said, any time you're using an object that implements IDisposable, it's a good practice to call Dispose() on the object as soon as you are through working with it.
For some of your specific points:
1) If you know you're "done" with the Form, you can call Dispose() on it. This will force a cleanup at that point in time of the unmanaged resources associated with the form.
2) In this case: if your object is just used in that method, use "using" instead:
using (MyObject myObject = new MyObject())
{
// use your object
} // It'll be disposed of here for you
3) There are rare reasons to do this, but in general, no.
4) Events are a delegate - the memory associated with the delegate will be collected after the delegate itself becomes unrooted, which typically happens when the objects in question are unrooted.
Upvotes: 10
Reputation: 14919
Look at this question:
Is there a common practice how to make freeing memory for Garbage Collector easier in .NET?
If your class instantiates the IDisposable interface, that (probably) means that it has system resources that have to be disposed of directly. One easy way to accomplish that is to use the using keyword, as in:
using(var g = Graphics.FromBitmap(bmp))
{
//Do some stuff with the graphics object
}
per @Matt S's answer in that question I referenced.
For your questions:
Upvotes: 2