Detecting Memory Leaks in ASP.NET [closed]

我只是一个虾纸丫 提交于 2019-11-27 17:15:38

I previously posted this guide in response to another question, but that question and my answer appear to have been deleted. It's not for the faint of heart:

  1. Install the Debugging Tools for Windows (Available as part of the Windows SDK) on the server
  2. When the application has been running for a while, use adplus to capture a memory dump of the process (It's useful to use something such as Process Explorer to find the correct process ID to dump):

    ADPLUS -hang -p <process id> -o .

  3. This will create a directory containing the memory dump. You can now use windbg, and open the dump file (File -> Open Crash Dump...)

  4. The joys of unmanaged code now appear. But you use something called Son of Strike, which understands .NET code, to see what objects are allocated. First you load SOS:

    .loadby sos mscorwks

And then you ask it to examine the managed heap:

!dumpheap -stat

This generally spews a ton of output, but there are two columns showing the number of instances and the amount of memory being consumed, by type. Some types you expect to see a lot of (e.g. String), but if, say, there are thousands of instances of one of your own types, you might be leaking these objects somehow. One that's caught me in the past is hooking up an event handler in an object to a static event in the application - that event then has a live reference to every one of those objects.

I can never remember how most of this works, and generally refer to this cheat sheet for SOS

Tess Ferrandez has a good blog which sometimes covers .NET debugging using the unmanaged debuggers


E.g. a post from last May, detailing a potential problem if you use XmlSerializers with a non-default constructor.

There are many memory profilers out there.

One popular one is DotTrace, another is the ANTS memory profiler, both are commercial offerings.

You can get down to a very low level without paying for a third party tool. This is not for the faint of heart though.

Get Started: Debugging Memory Related Issues in .Net Application Using WinDBG and SOS

Memory Leak Detection Using Windbg

A .Net memory leak for ASP is going to be limited to anything that persists. Application state and to a lesser extent, session state.

Anything that works within these areas are the first to check.

Also, static objects in any class, especially lists or anything of the sort.

You can also try using the asp.net web profiler. It is a free tool which allows you to view information as it is stored in memory while the application is running.

This allows you to analyze the asp.net cache, view all the current sessions and contents of the application state.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!