How to find where memory is used in C #?

I have C # XNA in a WP7 project and I find that it eats memory between screen changes and does not return it, which ultimately leads to an outofmemory error.

I looked and looked, but I can’t let my life find where this memory goes.

Is there a way to find out where the memory is used and why it does not return to the device?

Thanks for any help!

+7
source share
6 answers

Use the Microsoft CLR Profiler for .NET Framework 4 (free) in your Windows project version.

Using this, you can get the time frame for allocating project memory. Or you can check a bunch. It gives you a list of all selected by type. You will probably see an object that you over-distribute, from there you can call up a distribution graph for this type or that time range. This will show what functions these objects have allocated.

Here's a random blog entry that has screenshots and a discussion of the CLR Profiler in action. (Not exactly what you will do with it, but a useful introduction if you have never used the CLR Profiler before.)

However . Since you use XNA, and it is usually very difficult for you to try to get C # to work from managed memory, you have probably run out of unmanaged memory. Somewhere you do not call Dispose() before you stop using the graphic or sound object you created? I discussed the details of this pair .

Therefore, just keep in mind that if you have very small objects appearing in the CLR Profiler, they can actually use a huge amount of unmanaged memory.

+6
source

You can try Redgate ANTI Memory Profiler (but it costs), and I'm not sure about using it with WP7, but this is for C #.

There is a free trial, so you can use it to find the problem.

+2
source

Eqatec have a profiler that works with WP7. This is not a memory profiler, but I would try to understand what it found. It can help you in the right direction.

+1
source

The coding4fun toolkit contains a memory counter that helps track the memory usage of your application. Here is the documentation and article demonstrating its use.

+1
source

Use the CLR Profiler for the .NET Framework 2.0 . It is not supported in XNA 4.0 by default, but Dave on Crappy Coding has a workaround .

+1
source

I am using Monoprofile . It has various options; simplest use

 mono --profile=log program.exe 

And then, after exiting program.exe , it will leave the profiler file ( output.mlpd by default) and read the collected information:

 mprof-report output.mlpd 

eg. I am doing mprof-report output.mlpd | vim - mprof-report output.mlpd | vim - .

By default, it collects a bunch of different information. At the very beginning of the output (with the given default settings) you will see a table of functions sorted by the column "selected", for example. snip:

 Allocation summary 24 Bytes Count Average Type name 25 7357392 306558 24 System.IntPtr 26 6677904 139123 48 System.Collections.ArrayList.ArrayListEnumeratorSimple 27 5842736 136185 42 Mono.Unix.Native.Syscall._pollfd[] 28 3078176 49566 62 System.Byte[] 29 2574504 38057 67 System.String 30 908320 14803 61 System.Int32[] 31 719984 5294 136 Mono.Globalization.Unicode.SortKeyBuffer 

Its advantages are out of my mind:

  • It is cross-platform, so you can easily profile .net RAM allocations on GNU / Linux and Mac.
  • It was developed by the creators and largest users of .net - Microsoft. It was previously developed by Xamarin, but MS bought them, and now they mentioned Mono on the main page.
0
source

All Articles