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!
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.
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.
Eqatec have a profiler which works with WP7. It's not a memory profiler, but I'd give that a try to see what it shows up. It may help point you in the right direction.
The coding4fun toolkit contains a Memory Counter that helps track memory usage of your application. Here's the documentation and an article demonstrating its use.
Use the CLR Profiler for the .NET Framework 2.0. It's not supported in XNA 4.0 by default, but Dave on Crappy Coding has a workaround.
The one I use is Mono profiler. It has various options; the simplest usage is
mono --profile=log program.exe
And then, after program.exe
would exit, it'd leave a profiler file (output.mlpd
by default), and to read the gathered information use:
mprof-report output.mlpd
E.g. I do mprof-report output.mlpd | vim -
.
By default it collects a bunch of different informations. At the very beginning of the output (given default settings) you will see the table of functions sorted by «allocated» column, e.g. a 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 out of my mind:
- It is crossplatform, so you could easily profile .net RAM allocations on GNU/Linux and Mac either.
- It is developed by creators and largest users of .net — Microsoft. Earlier it was developed by Xamarin, but MS bought them, and now they mentioned on the main Mono page.
精彩评论