Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a .NET Memory Leak?

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++.

like image 962
user691585 Avatar asked Apr 04 '11 18:04

user691585


People also ask

How do I find a visual VM memory leak?

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.

How do you detect 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.

How do I find a memory leak in Visual Studio?

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.


3 Answers

In VS2010 try the Analyze > Launch Performance Wizard menu option and choose the memory option.

In VS2017 it is Analyze > Performance Profiler...

like image 92
Josh M. Avatar answered Oct 11 '22 17:10

Josh M.


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.

like image 37
BrandonZeider Avatar answered Oct 11 '22 15:10

BrandonZeider


As alternative to the embedded performance profiler of VS, I find Jetbrains' DotMemory practical and easy.

  • You can see very quickly which objects types, from managed code that are leaking.
  • You got a dashboard with a view on common problems (string duplicates etc)
  • You also have a view on the unmanaged memory. The interface is rather complete, complex (you can really dig and understand many things, and find quickly the culprit objects and code). a view on a snapshot analysis [0] https://www.jetbrains.com/dotmemory/
like image 35
Soleil Avatar answered Oct 11 '22 15:10

Soleil