Green_qaue
Green_qaue

Reputation: 3661

LibGdx when and how should I dispose assets using an AssetManager?

Say I have a method that loads all my assets for a Screen, this method is called in that Screens constructor:

public void load(){
    manager.load(pd_bg, Texture.class, textureParams);
}

Now when I exit that Screen, I have e method that unloads all these assets:

public void unLoad(){
    manager.unload(pd_bg);
}

Inside my Screen I might use this asset for a Sprite, like so:

Sprite bg = new Sprite(GdxAssetManager.manager.get(GdxAssetManager.pd_bg, Texture.class));

Finally, do I need to dispose of the texture used in this sprite even though I call the unLoad() method? i.e:

public void dispose(){
    GdxAssetManager.unLoad();
    bg.getTexture.dispose(); //Is this line needed?
}

I am also wondering, if I load all resources when I start the app, should I then unload resources when I exit a Screen? How will they be loaded up next time then (since I only load the on launch)?. I am using a Sprite as an example, but I guess the answer will be true for any asset.

Upvotes: 1

Views: 2069

Answers (2)

Kleysley
Kleysley

Reputation: 573

To answer your last two questions: In your launcher class, you should load every texture you need from all screens. Then in your screens you aceess the images you need and you dont unload them and you also don't dispose them nor do you dispose the assetmanager.

Then in your launcher class in the

dispose(){}

method you first unload evereything and then call

assetmanager.dispose();

Upvotes: 0

Xoppa
Xoppa

Reputation: 8113

No, you don't have to (and must not) dispose it when you unload it. In general, the rule of thumb goes: if you create it, you destroy it. So if you create a Texture (by using the new keyword) then you own that texture and are responsible for destroying it (calling its dispose method). In the case of AssetManager, it is the AssetManager who owns the resource and is responsible for destroying it.

To keep track of which resources needs to be created and destroyed, AssetManager uses reference counting. So it is important that you eventually call unload for everytime you call load.

And because you created the AssetManager using the new keyword, you own it and you are responsible for calling its dispose method:

public class MyGame extends ApplicationAdapter {
    public AssetManager assetManager;

    @Override public void create() {
        assetManager = new AssetManager();
        ...
    }
    ...
    @Override public void create() {
        assetManager.dispose();
        assetManager = null;
        ...
    }
}

Have a look at the documentation: https://github.com/libgdx/libgdx/wiki/Managing-your-assets

Btw, in your code it looks like you are using a static AssetManager. Although not related to your question, be aware that will lead to issues. So I'd advice you to implement a proper object oriented design instead of making things static.

As for your second question, it is unclear what you mean. If you mean when you should call AssetManager#unload, then the answer is whenever the class that called the corresponding AssetManager#load method no longer needs it.

For example, if you have an asset named "image.png" and you call the assetManager.load("image.png", Texture.class) in both your MyGame and MyScreen classes then you should call assetManager.unload("image.png") also in both your MyGame and MyScreen class.

Upvotes: 6

Related Questions