I have created a sample program, with 2 Dialogs. Both Dialogs have a picture box, one calls pictureBox1.Image.Dispose(); in the protected override void Dispose(bool disposing) method and the other doesn't.
When you run the program and use the Task Manager to look at the memory usage, it becomes very obvious that the dialog that does not call pictureBox1.Image.Dispose(); leaks memory really badly.
Does anyone have a suggestion for a tool that would show up the problem in such a clear way?
Is there any way to count memory allocation/deallocation for a particular c# source file? At least this was something I was able to do with unmanaged C++.
With Java VisualVM, we can memory-monitor the Java Heap and identify if its behavior is indicative of a memory leak. After just 30 seconds, the Old Generation is almost full, indicating that, even with a Full GC, the Old Generation is ever-growing, a clear sign of a memory leak.
The best approach to checking for the existence of a memory leak in your application is by looking at your RAM usage and investigating the total amount of memory been used versus the total amount available. Evidently, it is advisable to obtain snapshots of your memory's heap dump while in a production environment.
The primary tools for detecting memory leaks are the C/C++ debugger and the C Run-time Library (CRT) debug heap functions. The #define statement maps a base version of the CRT heap functions to the corresponding debug version.
In VS2010 try the Analyze
> Launch Performance Wizard
menu option and choose the memory option.
In VS2017 it is Analyze
> Performance Profiler...
Check out Ants Profiler: http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/
There is a 14 day free trial and lots of helpful tutorials to get you started.
As alternative to the embedded performance profiler of VS, I find Jetbrains' DotMemory practical and easy.
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