# Why am I seeing unbounded memory usage in my WP8 game?

I recently upgraded to VS2012 and tried my game out on the new WP8 emulators. I was dismayed to find out the emulator now crashes and throws an out of memory exception during my sprite loading procedure (it still works in WP7 emulators and on my WP7).

Regardless of whether the problem is the emulator or not, I want to get a clear understanding of how I should be managing memory in the game.

My game consists of a character that has 4+ different animations. Each animation consists of about 4 to 7 frames. On top of that, the character has up to 8 stackable visualization modifications (eye type, nose type, hair type, clothes type, et cetera). Prior to my memory issue, I preloaded all textures for each animation frame and customization and created animate action out of them. The game then plays animations using the customizations applied to that current character.

I revisited this implementation when I received the out of memory exceptions and have started playing with RenderTexture instead, so instead of pre-loading all possible textures, it on loads textures needed for the character, renders them onto a single texture, from which the animation is built. This means the animations use 1/8th of the sprites they were before. I thought this would solve my issue, but it hasn't.

Here's a snippet of my code:

var characterTexture = CCRenderTexture.Create((int)width, (int)height);

characterTexture.BeginWithClear(0, 0, 0, 0);
// stamp a body onto my texture
var bodySprite = MethodToCreateSpecificSprite();
bodySprite.Position = centerPoint;
bodySprite.Visit();
bodySprite.Cleanup();
bodySprite = null;
// stamp eyes, nose, mouth, clothes, etc...

characterTexture.End();


As you can see, I'm calling CleanUp and setting the sprite to null in the hope of releasing the memory, though I don't believe this is the right way, nor does it seem to work.

I also tried using SharedTextureCache to load textures before stamping my texture out, and then clearing the SharedTextureCache with:

CCTextureCache.SharedTextureCache.RemoveAllTextures();


But this didn't have an effect either.

I used VS to do a memory profile of the emulation causing the crash. Both WP7.1 and WP8 emulators peak at about 150mb of usage. WP8 crashes and throws an out of memory exception.

Each customisation/frame is 15kb at the most. Lets say there are 8 layers of customization (120kb) but I render then onto one texture which I would assume is only 15kb again. Each animation is 8 frames at the most. That's 15kb for 1 texture, or 960kb for 8 textures of customization. There are 4 animation sets. That's 60Kb for 4 sets of 1 texture, or 3.75MB for 4 sets of 8 textures of customisation.

So even if its storing every layer, its 3.75MB. That's nowhere near the 150mb breaking point my profiler seems to suggest.

WP 7.1 Memory Profile (max 150MB)

WP8 Memory Profile (max 150MB and crashes)

Why might I be seeing this behavior?

First, try to avoid creating objects on every loop/frame. I hope the statement var characterTexture = CCRenderTexture.Create((int)width, (int)height); is in an init, and not in the visit() or draw() methods of an object which inherits CCObject.