Wednesday, February 8, 2012

Cocos2D iPhone - will this save memory?


When you create a sprite on cocos using a texture you allocate memory.



Something like:




CCTexture2D *textureProxy = [[CCTexture2D alloc] initWithImage:image];
CCSprite *proxy1 = [CCSprite spriteWithTexture:textureProxy];



My question is: suppose I want several sprites using the same texture and I do




CCSprite *proxy1 = [CCSprite spriteWithTexture:textureProxy];
CCSprite *proxy2 = [CCSprite spriteWithTexture:textureProxy];
CCSprite *proxy3 = [CCSprite spriteWithTexture:textureProxy];
CCSprite *proxy4 = [CCSprite spriteWithTexture:textureProxy];
CCSprite *proxy5 = [CCSprite spriteWithTexture:textureProxy];



Will I use 5 times more memory? (or in other words, will the texture be duplicated to each proxy or is the sprite just a "empty box" that references the original texture?



If the later is the answer, then the memory usage for similar sprites are not that huge, right?



thanks.

2 comments:

  1. Sprites keep only references to textureProxy. Even if you create each sprite with spriteWithFile:, file names are cached in CCTextureCache.

    ReplyDelete
  2. In cocos Different memories are allocated for texture and variables. So if u keep on creating just variables using same texture won't add up texture memory multiple times. And CCSprite objects are autoreleased objects.

    ReplyDelete