5
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\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
    Commented Apr 25, 2017 at 16:12

2 Answers 2

1
\$\begingroup\$

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().

\$\endgroup\$
4
  • \$\begingroup\$ @downvoter will you please tell me the reason what is wrong with my answer \$\endgroup\$ Commented Nov 13, 2017 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
    Commented Nov 13, 2017 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\$ Commented Nov 13, 2017 at 14:41
  • \$\begingroup\$ @Digvijaysinh Gohil nothing is wrong, your answer explains well and is very good +1 from me \$\endgroup\$
    – Joza100
    Commented Jul 30, 2018 at 13:44
0
\$\begingroup\$

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);
    assetManager.finishLoading();
    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.
    //assetManager.dispose();
    //atlas.dispose(); 
}

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

@Override
public void dispose(){
    assetManager.dispose();
    atlas.dispose();

    //Dispose it here with other resources.
}
\$\endgroup\$
2
  • \$\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
    Commented Feb 17, 2016 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
    Commented Feb 18, 2016 at 9:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .