waylonion
waylonion

Reputation: 6976

Is it wasteful to repeatedly reload the same spritesheets into the frame cache?

Currently, I have a menu scene in which I add the spritesheets to the framecache. I repeatedly go away from the menu scene and go back. Thus, every time the menu scene is reloaded, the same spritesheets are added to the framecache. Is this a bad thing? Are the old ones overwritten, ignored, updated, kept? Am I leaking memory or wasting memory by readding the same spritesheets to the framecache over and over again?

Upvotes: 1

Views: 154

Answers (1)

YvesLeBorg
YvesLeBorg

Reputation: 9089

It is neither wasteful nor harmful. The key components behind all this are the CCTextureCache and CCSpriteFrameCache singletons. They are both using an NSMutableDictionary, under the hood. When you 'reload' a texture, if it is already keyed into the cache, the 'add' action is silently omitted (ie the texture will NOT be reread and reloaded), and the invoking method is returned the appropriate texture from the one currently in cache.

For the sprite frame cache, when the .plist is read, a sprite frame object is created, the embedded CCSpriteFrame is set for each key in the plist. Thus, when you reload, all previous CCSpriteFrame objects will be released (assuming you are not retaining them somewhere else in your code, like for example if you retain a CCAnimation that refers them).

The actual resources you have to concern yourself with are time and memory. When you leave the menu, if the 'destination' is memory constrained, you could remove unused textures and unused sprite frames before allocating the resources required there. Having done that, when you come back to the menu, the texture will be re-read and re-loaded (there will be very little extra impact for the CCSpriteFrame's). You must determine for yourself whether this introduces an unacceptable lag, and base your decision to remore unused textures on that ... but please, do that on a device, not the simulator.

Upvotes: 2

Related Questions