2

I would like to know how each of the marked lines of code affect memory consumption. Given that my sprite sheet takes say 4MB in memory.

CCSpriteBatchNode *spritesBgNode;      // Line 1
spritesBgNode = [CCSpriteBatchNode batchNodeWithFile:@"sprites.pvr.ccz"]; // Line 2
[self addChild:spritesBgNode];    // Line 3
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sprites.plist"]; // Line 4

sprite1 = [CCSprite spriteWithSpriteFrameName:@"sprite1"];  // Line 5
[spritesBgNode addChild:sprite1]; // Line 6 

[spritesBgNode addChild:sprite1]; // Line 7 

[spritesBgNode addChild:sprite1];  // Line 8

[spritesBgNode removeChild:sprite1]; // Line 9

[spritesBgNode removeSpriteFramesFromFile:@"sprites.plist"]; // line 10

[self removeChild: spritesBgNode]; // Line 11

i) At which line(s) does the 4MB sprite sheet start consuming memory?

ii) Does Line 5 lead to any extra memory consumption?

iii) What happens in the case of Line 8 (Line 7 added again), how does it affect memory?

iv) How does Line 9 and Line 10 affect memory consumption? Do they free memory?

v) If the batch node will not be used for a while is Line 11 advisable? What are the implications of adding it again later.

NSCodeman
  • 51
  • 4
  • If you want to know down to precise lines and bytes you should use Instruments. The texture uses 99% of the memory, the other lines add at most a couple hundred bytes extra. – CodeSmile Jul 07 '13 at 09:05

1 Answers1

0

i) Spritesheet consumes memory when it is loaded into memory. As coco2d is an open-source framework, you can check the code and you will see, that spritesheet is added into the memory after batchNodeWithFile: call. In will add it to the cache inside this method.

ii) your line 5 returns simple autoreleased object. It is common practice in objective-c. Object will be released automatically by release pool at the ent of the tick. It means that object will be destroyed if you not retain it, for example, by adding as a child to some other node.

iii) You cannot add child twice. It will cause assert if the parent of the node, that you are trying to add is not nil. As I said before, you can see it in the code of CCNode class.

iv) After textures are loaded into the memory, all sprites are very lightweight as any sprite just use link to the some part of texture to draw it. So you will free memory a bit, but you will still see your 4MB+.

v) After you remove any node from the parent, it's texture will be still loaded into the memory, even if object is destroyed. You can unload this texture by calls

[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];

Also if you use some animations, you can remove them from CCAnimationCache. Anyway, if you don't have problems with memory, I can offer not to unload textures from memory. Loading textures is the most long process, so you will have lag before draw spriteframe from unloaded texture, because it will be loaded again.

Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
Morion
  • 10,495
  • 1
  • 24
  • 33