How to identify memory leaks in c#? [duplicate]
Possible Duplicate:
Anatomy of a “Memory Leak”
Hi All what are the best practices for identifying memory leak in c#?
How about
ANTS Memory Profiler™: Profile the memory usage of your .NET application
ANTS Performance Profiler for optimizing your .NET code
Memory Leak Detection in .NET
First, install the latest version of the Microsoft Debugging Tools. Its a prerequisite.
To analyze memory consumption, I recommend you to download & use .Net Memory Profiler. It really simplifies all the process.
MemProfiler allows to analyze managed memory utilization from managed applications, windows services, asp.net apps and from memory dumps.
To find a leak on a process on a DEV machine, run MemProfiler and simply attach to the desired process. Once you are profiling, take a snapshots and MemProfiler will tell you which and how many instances are loaded, so you could know what is allocating your memory. Take at least 2 memory snapshots on different moments and compare them to see the “delta instances”. ( If delta is >0 then N new instances were created. Otherwise, delta<0, they were destroyed)
When to collect the memory dumps?
- You may take the first one after all the “required stuff” has been loaded, in example, after your app has responded successfully to a few requests.
- You probably will make a load test to the app before taking the second snapshot, because if the app has leakead A LOT, then its easier to find out where.
- You can also continue to load test the app and take a 3rd, 4th snapshots, just in case.
To collect a memory dump on web application on a production environment:
You are not required to install the Debugging tools on the server. You can install it on a developer pc and then copy the folder to the production environment. E.g c:\Program Files (x86)\Debugging Tools for Windows (x86)
To identify the Asp.Net w3wp use this command (works with W7 and Server 2008, didnt tried with 2003) :
%windir%\system32\inetsrv\appcmd list wp
To generate the dump, execute:
AdPlus -hang -p <process id> -o <outputDir>
Other Tips:
- Generally your app will appear to leak strings, and in all the cases that I analyzed that was false. In my case, I used Asp.NET+WCF+IdentityModel, and they used a LOT of strings.
- If you are interested, do the “.NET Memory Profiler Tutorials” at http://memprofiler.com/tutorials/ and follow Tess Ferrandez blog http://blogs.msdn.com/tess/
If you think that probably there is a memory leak somewhere, but you have no idea where could it be, start by watching memory usage per process with Task Manager or Process Explorer, looking for a process that eats always more and more RAM:
Use AdPlus to generate the dump, avoid Task Manager, because Task Manager is a 32 bit process probably dumping a 64 bit one. MemProfiler gets confused in that case.
When analyzing the objects in the memory, you will see an ExecutionEngineException, StackOverflowException and OutOfMemoryException on the heap. Don’t Panic. They doesn't mean that your application are raising that exceptions, they are always pre-allocated just in case:
Use a memory profiler - attach it to the suspected process and run the suspected actions.
You can find some recommended profilers in the answers to this question.
Visual Studio Profiling/Performance Tools allow you to track Object Allocation and Lifetime.
Unfortunately, that's only available in the more costly editions of the VS hierarchy.
精彩评论