Reputation: 179
I have made a very simple game with three screens. The game is not really graphic intensive. I want to dispose of my disposable objects however, if I dispose them, this causes problems for when the game is restarted/the screen is revisited. My objects are simple Textures and sounds from my Assets class so I do not know if I can incorporate pooling here.
What is the best way to dispose of these objects and retrieve them when needed.
Thanks
Upvotes: 1
Views: 845
Reputation: 8584
I'm not an expert on the topic of memory management but these are my findings. I'm also assuming that you are using Screen
from LibGDX.
Whenever you leave the screen, either by showing a new Screen
or the Home
button, hide() is being called and assets might be disposed by the garbage collector. And obviously also when you dispose them yourself.
Whenever you show a screen show()
is being called and here you could load your assets again. If you do have a heavy load of assets it is advisable to use the AssetManager
but since you stated you don't you are probably just fine reloading assets in the show()
method.
I guess you are loading everything in the constructor of your screen. All you might need to do is copy the code of loading assets and setting up tables from there to the show()
method. The constructor is only called once so whenever android decides to dump your assets, when for example your app is not currently displaying your assets won't be reloaded. However the show method will be called and you should be fine with reloading everything there. If it takes miliseconds to load your assets then just load them there. Otherwise you can use the asset manager and just call finishloading()
on it, this won't take any time when your assets are still loaded and loads them when needed again.
Upvotes: 1
Reputation: 93609
Sounds like you are lazy loading your assets. Make sure you always load new instances when your game or screen restarts. Or set your references to null right after disposing them.
If you are returning to a Screen without creating a new instance of it, your loading of stuff that gets disposed needs to be done in the show()
method because the constructor is not called again.
Upvotes: 1
Reputation: 4997
Libgdx provide an AssetManager
, this allow you to
Initialisation example
AssetManager manager = new AssetManager();
manager.load("data/mytexture.png", Texture.class);
Retrieve a resource loaded
Texture tex = manager.get("data/mytexture.png", Texture.class);
Dispose a resource
manager.unload("data/mytexture.png");
Dispose all resources loaded with this AssetManager
manager.clear();
All informations about the AssetManager
is available on their github
Upvotes: 1