How to detect where a Memory Leak is?

2019-01-21 20:32发布

I have a large website that seems to be sucking up all the memory that is being allocated. There is nothing else on the server beside this site. Within a week it eats away the 2 gigs and requires a restart. Currently this is server 2008 32 bit using IIS 7. We are reinstalling to use 64bit and add more memory. It would be nice to be able to track down where the leaks are occurring.

So what is the best practice to tracking memory leaks?

12条回答
我命由我不由天
2楼-- · 2019-01-21 21:11

You can try using profilers such as dotTrace -- set it into a memory trace and run your application.

This should give you clues in which assemblies and areas of the application that eat up too much memory on the get go.

查看更多
劫难
3楼-- · 2019-01-21 21:11

Do you do any Office interop? If so, make sure you clean up your application objects. That's one possible culprit.

Another is global objects, such as anything that's static.

查看更多
做自己的国王
4楼-- · 2019-01-21 21:15

Run, don't walk, over to Tess Ferrandez's blog, If broken it is, fix it you should, which has well scripted labs dedicated to learning how to diagnose and debug crash, hang and memory issues with .NET code. She has some of the best material I've found to date to help get you started.

Commercial memory profilers such as ANTS and SciTech are excellent resources that will show what objects are in the heap, and how they are rooted. Most commercial memory profilers have the ability to load a memory 'snap' of a process (say from your production environment).

You can capture a memory 'snap' (see Snap v. Dump) using adplus.vbs or DebugDiag. Adplus is available as part of the Debugging Tools for Windows. DebugDiag will also have some rudimentary analysis (but seems to be more reliable on unmanaged code) automagically.

Monitor the Application
For an idea on what to monitor, see Improving .NET Performance and Scalability, specifically Chapter 15.

As to how to monitor, there are commercial tools available for that as well, however, every Windows machine also comes with Perfmon.exe, which can be used to record relevant performance counters.

Test the Application
For an idea on how to perform load, or stress, tests, check out the Patterns and Practices Performance Testing Guidance for Web Applications.

Debug the Application
Once you've identified you've got a problem (monitoring) and your able to reproduce the problem (testing) you can get down to debugging the problem. See the links for Tess - that information will carry you a long way.

Then rinse and repeat! :)

Good luck!
Z

查看更多
何必那么认真
5楼-- · 2019-01-21 21:16

This is probably prevention rather then detection, but at the c# code level, you should check that any classes that use large resources such as images and other files are correctly implementing the dispose pattern. If needed you may also need to override the finalizer.

MSDN has good guidance on this subject.

If you have any classes in your application that you know make use of large resources, these are the first places to look for memory issues.

查看更多
该账号已被封号
6楼-- · 2019-01-21 21:17

I've found that the EQATEC Profiler is pretty good, plus it's free!

查看更多
对你真心纯属浪费
7楼-- · 2019-01-21 21:23

Run a profiler on your code.

Here are two good options:

RedGate's memory profiler

Jetbrains dotTrace

I believe both products have a free trial.

查看更多
登录 后发表回答