I have a .net application (running as a windows service). From time to time it starts memory leaking (using 1GB+ of RAM). How do I find the cause of this memory leak? Which tools are there and how do you use them?
-
How much memory do you have available when it does that, and does it ever run out of memory at that point? – Lasse V. Karlsen Feb 15 '11 at 09:34
-
@Lasse, actually I have various services that start asphixiating the server as their memory consumption raises. So yes, the server does run out of memory. – Klaus Byskov Pedersen Feb 15 '11 at 09:41
5 Answers
I used WinDbg and the SOS-extesion with success for debugging memoryleaks in .NET applications.
Have a look here and here to get a start. I also recommend Tess Ferrandez' blog for more information about debugging .NET with WinDbg

- 99,095
- 21
- 171
- 219
you can start from here: http://msdn.microsoft.com/en-us/library/ff650691.aspx the tool is free and it worked for mr in the past, even if it is not easy to use. There is commercial options too, but never used.

- 32,832
- 9
- 75
- 115
In similar situations I use to pick up Ants memory profiler or similar and check the count of allocated objects for objects that are allocated more than expected. I especially look at forms and other objects with events bound to them. This sometimes gives a good start of what objects that are involved in the leak. From there it is the usual detective work to find out the "hidden" reference to those objects.

- 46,430
- 8
- 69
- 108
I used Ants once and it helped me to solve my problem. As Felice points, there are some free tools that can be used too.
Most of the time your memory leaks are from undisposed objects. Try wrapping all your connections in an using block. And if you are any COM+ objects (for example GDI+ objects - Bitmaps, etc.), call Dispose() always when you finish using them. Also forgetting to unregister event handlers eats a lot of memory.

- 21,988
- 13
- 81
- 109

- 4,020
- 1
- 24
- 27
I can recommend to use .NET Memory Profiler by Scitech (http://memprofiler.com/) for finding the memory leak in your .NET application.

- 3,816
- 3
- 23
- 32