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:
myform.showDialog
, show the details. The memory jumps from 60 KB to 105 MB.myform
to get back to grid, and dispose that form and set it to null. Memory remains at 105 MB.How can we free up the memory when we close myForm
?
We have already tried GC.Collect()
, etc., but without any result.
Description. Memory leaks are a class of bugs where the application fails to release memory when no longer needed. Over time, memory leaks affect the performance of both the particular application as well as the operating system. A large leak might result in unacceptable response times due to excessive paging.
Yes, there is a tool to analyze memory leaks.NET Memory Profiler. It is great to analyze memory leaks during development. It uses the concept of snapshots to compare new instances, disposed instances etc. This is a great help to understand how your service uses its memory.
Start the debug diagnostic tool and select 'Memory and handle leak' and click next. Select the process in which you want to detect memory leak. Finally select 'Activate the rule now'. Now let the application run and 'Debugdiag' tool will run at the backend monitoring memory issues.
The first place to look for leaks is in event-handling rather than missing Dispose()
calls. Say your container (the parent form) loads a child form and adds a handler for an event of that child form (ChildForm.CloseMe
).
If the child form is intended to be cleared from memory then this event handler must be removed before it is a candidate for garbage collection.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With