1

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.

3
  • Dont assume that a memory leak is the reason for Out Of Memory Exceptions. They are raised when a large enough contiguous block cannot be allocated. What is the module doing when it throws the OOMEX? Commented Jul 1, 2013 at 16:32
  • A memory trace/profile may not help you here. OutOfMemory exceptions normally have to do with compaction, rather than collection. Most of the time, all of the relevant memory is freed and is available to your computer, but the problem is space in the virtual address table for your process is not cleaned up. Normally, this is because of individual objects larger than 85000 bytes, such as byte arrays or large strings. Commented Jul 1, 2013 at 16:57
  • I know that the module throwing exception may not be the actual culprit. and it could be some another module occupying or creating enough LOH and is not re-claimed by GC.. and the requestiong program is not able to get enough contigeous memmory hence the error. I am not that familiar with dump analysis also.. Is there a good way to do that Commented Jul 1, 2013 at 17:13

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

dotTrace is a performance profiler. I guess you meant dotMemory: jetbrains.com/dotmemory
Also note: it cannot trace all memory allocations. Sometimes it says "Object skipped during profiling session"
Yes. Back in 2013 it was a single tool.
0

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.

Comments

0

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

Comments

0

You can try using the .NET memory allocation profiler in Visual Studio.

A blog post from the .NET Team shows you how to use it: .NET Memory Allocation Profiling with Visual Studio 2012.

About 1/3 of the way through the article, it shows how to run the .NET memory allocation profiler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.