简体   繁体   中英

How to find the cause of a memory leak in a .net application

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?

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

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.

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.

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.

我建议使用Scitech的.NET Memory Profiler( http://memprofiler.com/ )来查找.NET应用程序中的内存泄漏。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM