views:

183

answers:

5

What are the best way or method of best practise to ensure that a Winforms .NET application releases all the resources it consumed in the lifecycle of its execution?

In particular, the release of file handles and images.

Thanks.

+1  A: 

Follow these thumb rules

1 - Where ever possible, use using to ensure you properly dispose objects (especially when working with streams and all). You can apply 'using' to dispose any IDisposable

using (SteramReader reader=new StreamReader(filePath)) 
{
     //Do your stuff here
}

When you Dispose streams, Close will get called automatically.


2 - If you are not using Managed components, ensure that you are cleaning them up when you r objects get disposed or when your forms get closed


3 - If you are using too much XML Serialization, make sure you use the proper constructors, otherwise it can cause a memory leak - http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx


4 - Leave everything else to the the garbage collector

amazedsaint
A: 

If you can, put them in using blocks:

using(Bitmap bitmap = new Bitmap(path)) {
    ...
}

Then the resources of the bitmap will be guaranteed to be released at the end of the using block. Otherwise, use the Dispose pattern: http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

Ray Hidayat
+2  A: 

If your implementation is whiteboxed then calling Close on file stream should close it's memory stream.

If class implements IDisposable just use the using block if in C# so the resource will be disposed:

using (var foo = new Foo())
{
    // Do some stuff to foo
}

If you are writing a wrapper that will consume memory intensively then I recommend implementing IDisposable.

vanslly
What about if the code inside the using block throws exception?
MichaelD
By using the using block you are ensuring that the resource will be disposed, even if an exception occurs in the using block.
vanslly
The using block is translated by the compiler into a try..finally block with a call to foo.Dispose() in the finally. That way the compiler guarantees that Dispose will be called even if your code throws an exception.
Scott Dorman
Thanks guys. That's exactly what I want.
MichaelD
+1  A: 

If you're writing the class that has reference to files/images, it's your responsibility to provide a mechanism for these to be released.

For all .NET applications (not just WinForms), implementing IDisposable on any type that hold references to large file/memory resources is always a good start.

If you are making use of framework types that access resources, then as others have said, the using blocks are an elegant solution.

It is also possible to force the garbage collector to dispose of types at the time you request it to do so (deterministic), however it is strongly recommended you do not do this, and instead allow the garbage collector to decide for itself for both improved performance and memory managment.

Finally, because actually calling Dispose (directly or via using) is done by the user of your types, clearly documenting this (XML comments etc) is essential.

See this earlier SO question for much more detail

Ash
Yea, Im particularly interested in cleaning my own references up .
MichaelD
A: 

1.-Implementing IDisposable Interface will help you with the unmanaged resources. 2.- if you are programmatically accessing files in case of error try to Close() the files or streams in a try{}catch{}finally statement to avoid locks to that resource 3. the Garbage Collector will take of resources most of the times

Oscar Cabrero