简体   繁体   中英

How to trace memory allocation in C#

Are there any methods to trace how much memory is allocated, de-allocated and retrieved by GC for a particular module in C# .net ?

I want to trace out the possible memmory leaks in my module. I am getting occasional System out of memory exceptions in production.

You should use a memory profiler to profile memory allocations.

I've used JetBrains dotTrace , which has a nice mode of taking two snapshots at different times and showing which objects were allocated but not collected between those two snapshots. Allows for easy finding for memory leaks, where you keep allocating new instances and not collect them.

To view the difference between two application memory states, you can mark the start and the end of a time interval, then capture a difference snapshot which shows how much memory was allocated and released during the marked time interval. The view can be filtered to show only live, new, or dead objects, or the difference between new and dead objects.

A memory profiler is a good idea. You can also get a rough sketch using the PerformanceCounters See the msdn . You could then gather some stats on your prod environment if it's hard to reproduce locally.

you can try .NET Memory Allocation Profiling with Visual Studio. try this msdn article to show you how http://blogs.msdn.com/b/dotnet/archive/2013/04/04/net-memory-allocation-profiling-with-visual-studio-2012.aspx . About 1/3 of the way through the article it shows how to Run the .NET memory allocation profiler.

JetBrains, as mentioned, as well as .NET Memory profiler has helped me several times. If you have a memory leakage problem in WPF there are some good advice in this post (old but a lot is still valid):

http://blogs.msdn.com/b/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx

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