What I'm wondering:

How do you dispose of textures / spritebatches and the things you normally need to dispose of now?

Why I'm asking:

Because theirs no automatic dispose() function upon creation of the Main libGDX class. I don't remember details but there used to of been an @override dispose

I don't want a memory leak or whatever it's called when you don't dispose.

  • \$\begingroup\$ There dispose function is still there as of 1.9.6 (newest) in all classes extending ApplicationListener. Can you show us your class code? \$\endgroup\$ – Charanor Apr 25 '17 at 16:12

Make sure that you implement ApplicationListener in your main class in order to override dispose().

For example if MyGame is my main class then:

public class MyGame implements ApplicationListener {
    // put your Logic

Also libGDX provides the Game class that already implements ApplicationListener, so you can extend the Game class as follows:

public class MyGame extends Game {
    // Logic 

Once you follow one of the above approaches you are able to override dispose().

  • \$\begingroup\$ @downvoter will you please tell me the reason what is wrong with my answer \$\endgroup\$ – Digvijaysinh Gohil Nov 13 '17 at 14:32
  • \$\begingroup\$ It's best not to get too bent out of shape about downvotes. They happen sometimes, even to established users. If the person leaves a comment, you can use that to improve the answer. If they don't, all you can do is write the next question or answer to the best of your ability. Since upvotes count for more than downvotes, you'll recover from those little setbacks very quickly. \$\endgroup\$ – DMGregory Nov 13 '17 at 14:39
  • 1
    \$\begingroup\$ No I am not angry or anything about it, it is for my future post that i won't repeat my mistake again that's all \$\endgroup\$ – Digvijaysinh Gohil Nov 13 '17 at 14:41
  • \$\begingroup\$ @Digvijaysinh Gohil nothing is wrong, your answer explains well and is very good +1 from me \$\endgroup\$ – Joza100 Jul 30 '18 at 13:44

Some resources are not cleaned by the garbage collector. You need to dispose them manually, in order to save memory. You just need to call .dispose() You can find more information about libGDX memory management here. There is also a list with all resources that implement a disposable interface.

For example, you can dispose a Polygon Shape after using it to create your Fixture:

public void create() {
    FixtureDef fDef = new FixtureDef();
    Fixture fixture;
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(w, h, position, angle);
    fDef.isSensor = true;
    fDef.shape = shape;
    fixture = body.createFixture(fDef);
    shape.dispose(); //You can dispose the shape.No longer needed.

However, you have to pay attention before disposing something that is still needed in your code:

public void setWalkLeftAnimation() {
    assetManager.load("walk_left.pack", TextureAtlas.class);
    atlas = assetManager.get("walk_left.pack");
    walkingLeft =  new Animation(1/12f, atlas.getRegions());
    // Don't dispose it,you won't be able to render the animation.

Dispose the Asset Manager and Atlas in the proper dispose method:

public void dispose(){

    //Dispose it here with other resources.
  • \$\begingroup\$ I don't have an @override dispose. If i just make a method called dispose; how do i make it called upon exiting the program. \$\endgroup\$ – Kyle Meade Feb 17 '16 at 23:25
  • \$\begingroup\$ In order to be able to override the dispose method, all you have to do is just write the method as I showed you, as long you're extending your class to Application Adapter, Screen Adapter or implementing Screen. You can see a list of methods you can override in a class extending Application Adapter : libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/… If you want to dispose things and your class doesn't extends or implements anything, you can create your own dispose method and call it from the dispose method in your Screen. \$\endgroup\$ – Igor Lopes Feb 18 '16 at 9:07

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.