I've been fighting with XNA ContentManager and memory allocations for some weeks because I'm trying to port my game from XNA (Windows) to ExEn / Monotouch (iphone).

The problem is that after playing a few levels, my game exits unexpectedly on a real iPhone device (not simulator).

Profiling memory usage on Windows with CLRProfile, I found some useful stuff but I also found something I dont understand. If I use 2 ContentManagers (1 for shared assets and 1 for level assets), when profiling, "Allocated Bytes" grows and grows after level through level but Memory consumption measured by Windows Task Manager stays constant (down when I unload the content manager and up again when I load content). Obviously, I contentManager.Unload() when level ends. After a few levels my game exits unexpectedly on an iPhone device.

If I use 1 content manager, "CRLProfiler Allocated Bytes" stays constant on Windows and on the iPhone; I can play the game normally and it doesnt exit unexpectedly. I use the same assets level through level.

It seems like in ios (iPhone) when loading and unloading the same assets, it allocates memory and consumes all device memory, so the ios kill it.

Can anybody explain me how this really works?
I've read quite a bit, but I still don't understand what's going on.

  • 1
    \$\begingroup\$ Perhaps is a bug of the API you are using. Try a small test with MonoGame and see if it still happens. \$\endgroup\$
    – r2d2rigo
    Commented Jan 31, 2012 at 20:53
  • \$\begingroup\$ Yes, it was a bug of the API I was using. It's already fixed now. exen.codeplex.com/SourceControl/changeset/changes/15002 \$\endgroup\$
    – Vackup
    Commented Jun 26, 2012 at 13:35

1 Answer 1


The garbage collector might work in unpredictable ways. You probably want to do a GC.Collect() at the end of your level loading logic to ensure that all releasable memory is indeed released. I don't know if there are any difference between the GC behavior in the simulator or on the device, but make sure you are making it easy for your ContentManager to be collected by null'ing all references to it and the content itself.

You might want to use the Instruments profiling tool (or something else, CLR Profiler?) while running on the actual device to check the memory allocation / release patterns and see if it's indeed a memory-related issue.

As a rule of thumb, you should avoid allocating memory during gameplay (create all game objects ahead of time or have pools) so that you can avoid accidentally triggering the GC while the game is supposed to be interactive.

Finally, try reducing your problem to a minimum reproducible test case by removing all game logic, display, anything that isn't needed to make the whole thing crash, and make sure that it still crashes. This should either help you find what you did wrong or help you locate any bugs that might exist in MonoTouch / ExEn / any other frameworks you might use.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .