As far as I know when I load a Sprite or a scene using CSLoader all textures are automatically cached by TextureCache (Director::getInstance()->getTextureCache()). It’s fine, because caching is making game faster. But my game will have tons of graphics on different scenes.
So my question is: how is texturecache managing memory? If I’ll visit enough number of scenes game can exceed available memory. What then, will it delete not used textures?
Or should I release all spritesheet’s for a scene in destructor?
I believe the texture cache will keep the textures in memory until either you remove them manually or until the app receives a low memory warning, where it will remove any textures not being used at the time. I would advise removing the textures you no longer need in memory when the scene is replaced (in the destructor, as you said), but a better way is to have a low memory loading screen between scenes.
When you want to change scene, you create the new scene and then replace the current one. But the current scene is still in memory when you create the new scene. That means the textures used by the current scene and the next scene are in memory at the same time. If both scenes use a lot of memory, this could cause your app to be terminated for excess memory usage.
If you create a loading scene between the current scene and the next one, then only the textures used by the current scene and the loading scene will be in memory when you change to the loading scene. When the current scene is replaced by the loading scene, the current scene’s textures will be cleared (if you manually clear it in the destructor.) Then, when you create the next scene, only the loading scene and the next scene will be in memory. Then you switch to the next scene and clear the texture cache of any textures used by the loading scene.
But if your app does not use a lot of memory, you may be able to get away with just switching from scene to scene with no loading scene, and just unload the textures in the destructor.
Thanks for explanation. I don’t have that much resources on one scene, but instead I have a lot of scenes.
For example I have 10 scenes which I can switch with arrows. Ofc I can release all textures when I switch to another, but if It’ll be kept in memory scene will load a lot faster second time.
If for example my app will have 100 scenes, but resources from any 2 won’t exceed memory and I’ll switch them all the time can my app crash or will it rescue itself?
Having multiple scenes will not hurt your memory, having a lot of textures loaded into your memory does. You should organize your textures into atlas which can be shared across multiple scenes. If that is not possible, what I would normally do is to “slap” a loading scene between scenes, where the loading scenes will unload the textures from the previous scenes and load the ones for the next. Sure, it will load slightly slower, but having a slightly slower loading time beats the device closing your game due to low memory.
It’s probably best to try and have in memory only what the current scene needs. But if you want to keep other textures in memory to speed up switching scenes, that’s up to you.
I would recommend looking into how much memory your app is allowed to use, then using tools to measure your app’s memory usage to see if you are near the limit. This will give you a better idea of how much you can keep loaded at a time.