Can anyone please help me to understand the proper use of the dispose().
Does dispose() get called automatically or do I have to make a call to it?

In a game I'm writing, I use a texture atlas and an asset manager for all of the textures I use. I have an abstract class called Enemy and every type of enemy extends it. I have an arraylist that holds all of my enemies, and when an enemy is dead, I remove it from the arraylist. Do I need to make a call to the dispose() method in the individual enemy classes to remove the textures from memory before removing it from the arraylist or does it happen automatically or is that not how dispose() gets used period? (Each individual type of enemy class sets its own textures by using a TextureRegion)


Dispose() is not called automatically. You have to call it manually whenever you with to dispose a texture (or anything that implements disposable). The only dispose() that's called automatically is ApplicationListener.dispose() and Game.dispose(), since it implements ApplicationListener.

As for solving the memory leak I'd suggest only using AssetManager for ALL (textures, sound, atlases, skins etc.) of your assets. This way you only have to call AssetManager.dispose() on your AssetManager when you exit your program, and in turn this will dispose of all of your assets automatically.

I can also vouch for not removing objects once they die. Instead, re-use them if you need more enemies. Only remove them once you absolutely have to. Although this might be a bit overkill if your game is not very big, or you're not using a lot of enemies.

Hopefully this answered your question!


libGDX's Game.dispose() method gets called automatically when you quit the game. The Screen.dispose() method should be called manually when you want to exit a screen. When you want to dispose of your textures, you could call the dispose method in your Enemy class from the Screen or Game dispose methods. You should never explicitly call the Game dispose method yourself. However, if you meant the Texture.dispose() or TextureAtlas.dispose() methods then yes, you need to call these once for every texture or texture atlas, respectively, that you want to dispose of.

  • \$\begingroup\$ The Screens dispose method is not called automatically, as mentioned here \$\endgroup\$ – Springrbua Dec 29 '15 at 7:51

You don't need to to dispose those enemies,Instead use an zOrdering and hide the enemies from the screen and put a condition to prevent the enemies from rendering onto the screen ,If you want to reuse those dead enemies just set enemy.setZorder(1 or some greater value) :




  • setZorder method should be custom written since libgdx does not support setZorder unlike most other engines
  • setZorder method is also capable of preventing the enemy sprites in the array list to be rendered on the screen

If you need setZorder code sample I can post some sample codes here.

  • \$\begingroup\$ I'm not sure what setZOrder is but in my game code I recently set it up so that if an enemy dies, a boolean gets set so that it will no longer be updated, rendered or be hit by weapons or hit the player. I don't want to reuse enemies on the level after they die, they get created again on a new level. I keep having a memory leak though and I've been really struggling to solve it. I've made many changes and still have not solved it. I even posted another question here which might better explain things: programmers.stackexchange.com/questions/308371/… \$\endgroup\$ – ace106 Jan 26 '16 at 17:17
  • \$\begingroup\$ zOrdering is keeping the game sprites in different layers,for example background has zorder value=1 and if my enemy die i will set its zorder value to 0 that will make the enemy to hide behind the background,in libgdx drawing sprites happen in the order you call draw method but using zordering you can sort the sprites to be drawn on the screen that is background that has least z order value 1 it will be drawn first then another sprite that has z order value 2 will be drawn above background . \$\endgroup\$ – Shersha Fn Jan 26 '16 at 17:34
  • \$\begingroup\$ you can see this question i have answered about zordering gamedev.stackexchange.com/questions/103605/… .I have made a game tower defence using zOrdering concept .youtube.com/watch?v=8bST0JTpGnI \$\endgroup\$ – Shersha Fn Jan 26 '16 at 17:34
  • \$\begingroup\$ I looked over the code sample and its results will give me basically what I have happening right now but just done in a different way. So that will not solve my current issue with a memory leak and when to dispose of enemy textures and sounds. I watched the youtube video of your game too and it looks very good. My game is level based and some levels have enemies from previous levels and some levels don't. So level 1 and 3 might have the same type of enemies but level 7 doesn't. At the end of the level I clear the arraylist of enemies and the new level creates a new list with new enmies. \$\endgroup\$ – ace106 Jan 26 '16 at 19:48
  • \$\begingroup\$ I just don't know when I need to dispose of the textures and sounds the enemies use or if I don't need to worry about that and just dispose of the asset manager when the game is done. See I don't know if when I remove the enemies from the arraylist without calling dispose on those enemies first, if that means I'm creating memory leaks due to them being discarded from the game but also having ties to the textures and sounds. \$\endgroup\$ – ace106 Jan 26 '16 at 19:52

In Libgdx some objects need to be disposed, as they are not managed by the garbage collector. The dispose() methode is supposed to dispose all of this objects, to release their ressources. The objects, which need to be disposed are mentioned here.
The dispose-method of the Game/ApplicationListener is called by LibGDX automatically, while the Screens dispose() is not called automatically (This is mentioned in the Screen API).
The Screens should generally be disposed, when the user probably won't return to it for a longer time. This might be the case for a Splash-Screen which shows only when the game is started.
Also, every not disposed Screen should be disposed when the ApplicationListener/Game gets disposed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.