Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should the LibGDX dispose() method be used?

Tags:

java

libgdx

It is very unclear to me how the dispose() method in the LibGDX framework works and should be used. From what I have learned, after you do not need a certain resource, you must dispose to make sure your program runs optimal.

I am working on a mobile application and I have an AssetManager that loads all of the required assets at launch, in a special designated Loading Screen. The problem is I don't know when or if I should unload or dispose them.

When I started developing the app, I didn't have the Manager loading all of the assets at the start and it was taking the program too long to load all the assets of a Screen every time it was accessed.

like image 330
vladutelu Avatar asked Jun 16 '17 16:06

vladutelu


People also ask

What should I dispose of LibGDX?

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 Screen s should generally be disposed, when the user probably won't return to it for a longer time.

How do I dispose of a screen in LibGDX?

Just look through everything in your Screen and check whether it has to be disposed or not. If there is a dispose method, call it in dispose() of the Screen .


2 Answers

You can see dispose() method in some classes/Interface of LibGDX API

  • dispose() method of ApplicationListener Interface and of course inside his implemented classes ApplicationAdapter, Game

    dispose() method of ApplicationListener is lifecycle method and called when the application is destroyed. Any disposable resources that you created in create() method should be destroyed in this method.

  • dispose() method of Screen Interface and his adapter class ScreenAdapter

    If you want some resources for particular Screen then create that resources in show() method and destroy in this dispose() method but Screen's dispose() not called by ApplicationListener lifecycle method so you need to call by yourself. I prefer to call dispose() method by hide() method of Screen interface.

  • dispose() method of many classes (like SpriteBatch, Stage, AssetManager and many more..) of LibGDX API.

    In 1st and 2nd points I use a term disposable resource. How I know which is disposable object. In libGDX there is Interface Disposable, LibGDX classes use this interface to release resources obtained by some object.

  • dispose() method of Disposable Interface

    Any classes that implement Disposable interface need to be disposed.

This is all about dispose() method.


  1. If you're creating large game that needed lots of resources then keeping all in memory is not preferable so you need to unload some resources that you're not using.

    Let's suppose we're creating game having two game play one is gardening and one is type of 3 match game, both game play need lots of resources so when we move from one game play to other, unload previous game play resources from AssetManager and load new game play resources into AssetManager.

    You should keep only one object of AssetManager in your App and must be dispose in ApplicationListener's dispose() method.

  2. If you're working on small project/game, you can avoid loading and unloading resources, create object of AssetManager once in create() method, load required resources and use all over your game and dispose() when you exit your game by dispose() method of ApplicationListener.

like image 86
Abhishek Aryan Avatar answered Oct 31 '22 21:10

Abhishek Aryan


Game.dispose() or ApplicationListener.dispose() is called automatically when the game quits.

In those method you dispose things you constantly need like textures,music,sounds or assetManager which holds those objects.

class MainClass extends Game....

@Override
public void dispose(){ // this method is called when the game quits
   texture1.dispose();
   music.dispose();
   assetManager.dispose();
}

However if you no longer need a object and is disposable you should dispose it when you dont need it no more, an example would be Pixmaps.

class MainClass extends Game....

Texture texture;
@Override
public void create(){
    Pixmap pixmap = new Pixmap(width,height, Pixmap.Format.RGBA8888);

    pixmap.setColor(0,0,0,0.5f);
    pixmap.fill();

    texture = new Texture(pixmap);
    pixmap.dispose();

}

Screen, whenever you dont need a screen, you dispose it

class SplashScreen implements Screen....

public void changeScreen(){
   dispose(); //dispose the current screen
   setScreen(new Screen());
}

@Override
public void dispose(){ //this method doesnt get called automatically
  splashTexture.dispose();
}

You could instantiate a screen and keep it in memory and dispose it in Game.dispose() though

like image 43
centenond Avatar answered Oct 31 '22 21:10

centenond