Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find where memory is being used up in C#?

I have a C# XNA on WP7 project running and I'm finding that it's eating up memory between screen changes and not returning it, eventually leading to an outofmemoryexception.

I've looked and looked but I can't for the life of me find where this memory is going.

is there any way I can find out where the memory is being used and why it's not being given back to the device?

Thanks for any help!

like image 656
meds Avatar asked Mar 21 '11 22:03

meds


2 Answers

Use Microsoft's CLR Profiler for .NET Framework 4 (free) on the Windows version of your project.

Using this you can get a timeline of your project's memory allocations. Or you can inspect the heap itself. It gives you a list of everything allocated by type. You will probably see the object you are allocating excessively, from there you can bring up an allocation graph for that type or that range of time. This will show what functions allocated those objects.

Here's a random blog entry that has some screenshots and discussion of the CLR Profiler in action. (Not quite what you will be doing with it, but a useful intro if you've never used the CLR Profiler before.)

However: Because you are using XNA, and you generally have to try really hard to get C# to run out of managed memory, you are probably running out of unmanaged memory. Is there somewhere you are not calling Dispose() before you stop using a graphics or sound object you created? I have discussed the details of this a couple of times.

So just be aware that, if you have lots of very tiny objects showing up in the CLR Profiler - they may in fact be using up vast amounts of unmanaged memory.

like image 185
Andrew Russell Avatar answered Sep 22 '22 10:09

Andrew Russell


You could try Redgate's ANTS Memory Profiler (but it costs) and I'm not sure about using it with WP7 but it's for C#.

There is a free trial so you might be able to use that to help locate the problem.

like image 42
Tony Avatar answered Sep 23 '22 10:09

Tony