Search code examples
javaandroidandengineout-of-memoryscene

Completely remove scene in AndEngine


To switch between levels in my game I recreate my GameScene, but but when I switch levels about 3 or 4 times I get an errno 12 out of memory error. I need to be able to somehow delete the whole scene and stop it from running in the background. Is there a way to do this so I don't run out of memory?

EDIT:

This is my method in which I load the new scene.

public void loadGameScene(final Engine mEngine, final SharedPreferences sp, final String map) {
setScene(loadingScene);
ResourcesManager.getInstance().unloadMenuTextures();
mEngine.registerUpdateHandler(new TimerHandler(0.3f,
    new ITimerCallback() {
        public void onTimePassed(final TimerHandler pTimerHandler) {
        ResourcesManager.getInstance().unloadGameTextures();
        mEngine.unregisterUpdateHandler(pTimerHandler);
        ResourcesManager.getInstance().loadGameResources();
        GameScene gameScene = new GameScene();      
        gameScene.setSp(sp);
        gameScene.loadLevel(map);
        gameScene.loadMap(map);
        setScene(gameScene);
        }
    }));
}

I put in the line

ResourcesManager.getInstance().unloadGameTextures()

but the problem persists.


Solution

  • If you are correctly calling unload() on the atlas that you are using when you call ResourcesManager.getInstance().unloadGameTextures(), you shouldn't get an out of memory error.

    I don't know how your ResourceManager behaves, but you don't need to recreat all the atlas everytime you change a scene (and by recreating I mean calling new BitmapAtlas() or some variant).

    Your ResourceManager should create all the needed objects at the beggining of the game (this includes the atlas). Then when you need regions for a certain scene, you call atlas.load() on the atlas that have those regions and atlas.unload() on other atlas that you don't need.