Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Memory Leaks in ASP.NET [closed]

My development team is using ASP.NET 3.5 / 4.0 right now, and our sites are running on IIS 7.5. Recently, we've been having problems (about once a week) that are causing Out of Memory exceptions to be thrown in our ASP.NET applications. The "Solution" is to restart the application pool on our website. I say "Solution" because it's hardly a solution; it's more of a bandage that's just keeping our application pool running at a reasonable state. It seems to me that some application or many applications are leaking memory, which is building up over time and causing the out of memory exception. While I can set IIS to periodically restart the application pool, I'd rather know how I can detect the memory leaks in order to attempt the fix the program rather than keep applying band-aids. Are there any tools out there that can possibly detect and log memory leaks for ASP.NET applications?

Also, we really started seeing more of this problem when we switched to using Telerik's RAD controls. Has anyone else had problems similar to this using these controls?

Thanks,

Aaron

like image 925
Aaron Avatar asked Mar 11 '11 15:03

Aaron


People also ask

How do you prevent memory leaks in .NET applications?

If you have implemented a very long-running or infinite running thread that is not doing anything and it holds on to objects, you can cause a memory leak as these objects will never be collected. The fix for this is to be very careful with long-running threads and not hold on to objects not needed.

How do I detect a memory leak in IIS?

The first thing you should do when you encounter the high memory usage is to determine whether the memory of a worker process on an application pool in IIS is leaked or not. You can use Performance Monitor. For more information on using Performance Monitor, see Analyzing Performance Data later in this troubleshooter.

How do you check if there are memory leaks?

One way to check for memory leak is to press and hold down your Windows key and tap the Pause/Break key to bring up System Properties. Click on the Performance tab and check System Resources for the percentage of free or available RAM.

Which method is used to detect memory leak?

Memory profilers are tools that can monitor memory usage and help detect memory leaks in an application. Profilers can also help with analyzing how resources are allocated within an application, for example how much memory and CPU time is being used by each method.


5 Answers

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.

like image 195
Damien_The_Unbeliever Avatar answered Oct 05 '22 03:10

Damien_The_Unbeliever


There are many memory profilers out there.

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

like image 24
Oded Avatar answered Oct 05 '22 02:10

Oded


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

like image 45
rick schott Avatar answered Oct 05 '22 02:10

rick schott


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.

like image 38
Bengie Avatar answered Oct 05 '22 02:10

Bengie


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.

like image 34
user2178972 Avatar answered Oct 05 '22 03:10

user2178972