Green_qaue
Green_qaue

Reputation: 3661

LibGdx - Serious memory leaks when switching Screens

I have an UI with 4 Screens, my app is pretty heavy atm and starts off at about 250MB Ram usage, after I switch back and forth between Screens a few time, it rises fast, up to 800MB Ram before it crashes.

This is how I deal with disposal:

Each Screen extends MainScreen, where I have this dispose method:

@Override
    public void dispose()
    {
        stage.dispose();
        System.out.println("Disposing Main Screen");
    }

When setting a new Screen I do this:

dispose();
game.setScreen(new HighScoreScreen(game));

Now I load all my assets using the AssetManager at launch, which means I don't unload/dipose any resources in the Screens because that will cause the next Screen to show an empty black box instead of the asset that was disposed. But is this a problem? I have a lot of resources, for example 8 different BitmapFonts That are initialized each time I load a new Screen that extends MainScreen. However if I try disposing these I get an error the next time I try to initialize them.

The only things left that are diposable are textures/fonts, so is this what is causing my problem? Should I load/unload for each Screen? This seems like it would lead to a bad user experience with a lot of loading.

Upvotes: 0

Views: 593

Answers (1)

Fishy
Fishy

Reputation: 1303

As @TomGrill Games said, you should initialize your resources once, and use them from your main class. You might have a resources class, initialized in your main class, and call your resources from there. Your code might look something like:

game.setScreen(game.mainScreen);

or

game.setScreen(game.resources.mainScreen);

You would also do that with your sounds, and other resources.

Upvotes: 1

Related Questions