Memory management

We have tried two ways to control heap memory allocation to avoid memory overflow which result into a crash after sometime:

  1. We are using following statement when exiting the scene

                 this->removeAllChildrenWithCleanup(true);
         CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames();
         CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFrames();
         CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile("AICharacterExpressAnimation.plist");
         CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile("AICharacterThinkAnimation.plist");
         CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile("AICharacterAggressiveAnimation.plist");
         CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile("UserCharacterAnimation.plist");
         CCTextureCache::sharedTextureCache()->removeUnusedTextures();
         CCTextureCache::sharedTextureCache()->removeAllTextures();
    
                 CCDirector::sharedDirector()->purgeCachedData();
    
  2. We have compressed all pvr files to .pvr.ccz and loaded it, also compressed the individual png’s.

But it doesn’t seem to make any difference at all, so please suggest any other solution to control memory.

Thank you in advance.

It’s important that you control the life cycle of scenes.
Notice that while there are sprites that use a certain texture you won’t be able to remove it’s texture from the cache.

Usually you create a new scene and then pass it to CCDirector:

// here the current scene (this) has
// all it's sprites alive and drawing

CCScene* scene = NewScene::create();  // this calls its init method

// here you have 2 scenes in memory at the same time
// sprites from the 1st are still alive, trying to empty TextureCache won't work

CCDirector::sharedDirector()->replaceScene(newScene); //tell the director to draw the newScene

As you can see, if you try to remove textures in NewScenes create, constructor or init it won’t make it.
Trying to remove them in the onExit() method you’ll still have the sprites alive, so no luck either.

The important thing here is to be sure sprites are deleted somewhere before you clean up the cache.
CCScene inherits CCNode, which will call removeAllChildren on it’s destructor.

After calling removeAllChildren with cleanup you can safely clean up the cache.
I’d try to call that before creating the next one, where more memory gets allocated.