We are developing a big .NET Windows Forms application. We are facing a memory leak/usage problem in that despite we are disposing the forms.
The scenario is like:
- Our application is using 60 KB of memory with a list of records displaying in a grid.
- When the user clicks on a record it opens a form,
myform.showDialog
, show the details. The memory jumps from 60 KB to 105 MB. - Now we close the form
myform
to get back to grid, and dispose that form and set it to null. Memory remains at 105 MB. - Now if we again perform step 2, it would jump from 105 MB to 150 MB and so on.
How can we free up the memory when we close myForm
?
We have already tried GC.Collect()
, etc., but without any result.
I recently had a similar issue where a running timer kept the form in memory, even though the form was closed. The solution was to stop the timer before closing the form.
Some third party controls have errors in their code. It may not be your issue if you're using some of those controls.
Check that you have completely removed all the references to the form. Sometimes may happen that you have some hidden references done that you did not notice.
For example: if you attach to external events from your dialog, that is, to events of an external window, if you forget to dettach from them you will have a remaining reference to your form that will never disappear.
Try this code into your dialog (example bad code...):
And open and close the dialog many times, you will se the number of strings in the console grow for each call. This means that the form remains in memory even if you called to dispose (that should only be used for releasing unmanaged resources, i.e.: closing a file and things like this).
Disposing the forms is not necessarily a guarantee that you are not leaking memory. For example, if you are binding it to a dataset but you are not disposing of the dataset when you are done, you will probably have a leak. You may need to use a profiling tool to identify which disposable resources are not being released.
And btw, calling GC.Collect() is a bad idea. Just saying.
I have not seen your code but this is the most likely scenario:
1) Your form gets closed but there is a reference to it hanging there and cannot be garbage collected.
2) You are loading some resource that does not get freed up
3) You are using XSLT and compile it everytime you transform
4) You have some custom code that gets compiled and loaded at runtime
The most common source of memory leaks in Windows Forms applications is event handlers that remain attached after form disposal...so this is a good place to start your investigation. Tools like http://memprofiler.com/ can help greatly in determining roots for instances that are never being GC'd.
As for your call to GC.Collect
In order to make sure your GC collect really does collect as much as possible, you need to make multiple passes and wait for pending finalizers, so the call is truly synchronous.
Also, anything that holds on to your instance of your form will keep the form around in memory, even after it's been Closed and Disposed.
Like: