I have a problem with memory management in cocos2d project. For my app I need to build all my animations before the interaction beginning. I have three animation. Each one have like 20 png file with a resolution of 1300 x 1000 px and have a size of 200ko.
So in my initialization i just wrote :
self.animation = [CCAnimation animation];
[_animation setDelayPerUnit:0.04];
for(int i = 1; i <= 20; i++)
{
[_animation addSpriteFrameWithFilename:[NSString stringWithFormat:@"image(%d)@2x.png",i]];
}
[[CCAnimationCache sharedAnimationCache] addAnimation:_animation name:@"animationWin"];
This kind of code is executed three times. If I run my app, it crashes directly. If i just pre-load 2 animations I have no crash. If I set breakpoints inside my for loops I can see that it crashes inside it.
I don't know how to solve this problem ... do you have an idea ?
Thanks a lot for your help !
Each one have like 20 png file with a resolution of 1300 x 1000 px and have a size of 200ko.
Let's do some math, shall we?
Under the assumption that the PNG files are 32-bit and you do not have NPOT (non-power-of-two) support enabled, each texture consumes:
1300x1000 => expanded to power of two => 2048x1024 => number of pixels => 2,097,152 => times 32-Bit (4 Bytes) color depth per pixel => 8 Megabytes => times 20 textures => tadaaa … a whopping 160 MB texture memory usage.
Well there's your problem!
The file size of the PNG doesn't matter. It's a compressed format. But textures are uncompressed, unless you switch to PVR which are really well compressed but you also get artifacts and discoloration.
Definitely best solution is to tune down the size and/or amount of artwork you're trying to load.