Shyam K Pananghat
Shyam K Pananghat

Reputation: 13

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.

Upvotes: 1

Views: 3133

Answers (4)

terrybozzio
terrybozzio

Reputation: 4530

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.

Upvotes: 0

Niklas Bjorkman
Niklas Bjorkman

Reputation: 688

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

Upvotes: 0

doctorlove
doctorlove

Reputation: 19282

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.

Upvotes: 0

Jakub Konecki
Jakub Konecki

Reputation: 46018

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.

Upvotes: 1

Related Questions