0
\$\begingroup\$

In a game I'm writing using LibGdx, I'm wondering if a memory leak will occur on Android when the home key is pressed or a phone call gets received and the game gets put into pause.

I have a Singleton class which holds all of my games assets and I use a static method to get access to it.

public class Assets
{
    private static Assets assetsInstance;

    private Assets()
    {
        //load assets....
    }

    public static Assets getInstance()
    {
        if (assetsInstance == null)
        {
           assetsInstance = new Assets();
        }
        return assetsInstance;
    }

    public void nullAssets()
    {
        //here things are set to null
        assetsInstance = null;
    }
}

In my Main class:

public class Main extends ApplicationAdapter
{
    public static Assets gameAssets;
    private GameStateManager gsm;
    public void create()
    {
        gameAssets = Assets.getInstance();
        gsm = new GameStateManager(this);
    }

    public void pause()
    {
        gameAssets.nullAssets();
        gameAssts = null;
    }
}

My GameStateManager looks like this:

private int currentState;
private Main m;
private GameState[] gameStates;
private final in NUMBEROFSTATES = 6;
public static final int TITLESTATE = 0;
public static final int PLAYSTATE = 1;
//more states

public GameStateManager(Main m)
{
    this.m = m;
    gameStates = new GameState[NUMBEROFSTATES];
    currentState = TITLESTATE;
    loadState(currentState);
}

My GameState class:

public abstract class GameState
{
    protected GameStateManager gsm;
    protected Main m;

    protected GameState(GameStateManager gsm)
    {
        this.gsm = gsm;
        m = gsm.m;
    }

    //abstract methods
}

The game states all extend GameState.

By calling pause() in the Main class which sets gameAssets to null and also assigning assetsInstance to null, does that take care of everything needed to let the garbage collector remove the singleton? Or does the fact that GameState class has m = gsm.m; mean something will remain in the memory and become a problem?

\$\endgroup\$
  • \$\begingroup\$ I vote to close this question as offtopic, because the core of the question is not related to game developement. \$\endgroup\$ – Bálint Jun 1 '17 at 5:26
  • \$\begingroup\$ How is it not related to game development? I'm developing a game and would like to know if this is a possible memory leak issue for when the game enters pause() on Android? I didn't specify that but perhaps I should have. I updated the question. Hopefully that is better. \$\endgroup\$ – ace106 Jun 1 '17 at 5:45
  • \$\begingroup\$ Just because you're developing a game, it doesn't make this automatically ontopic. It's not relevant to the question, it can be answered without that knowledge. \$\endgroup\$ – Bálint Jun 1 '17 at 5:48
  • 1
    \$\begingroup\$ Here is the most recent meta discussion on the subject of "general programming" questions, for reference. Note the lack of a strong consensus in either direction. \$\endgroup\$ – Josh Jun 1 '17 at 6:16
  • 1
    \$\begingroup\$ You're asking if this has memory issues - why not profile and test it? \$\endgroup\$ – Charanor Jun 1 '17 at 7:20
1
\$\begingroup\$

In Theroy, when there is no longer anything pointing to the old assetsInstance, all of the assets in it should be eligible for garbage collection.

But note that if any of the assets which your Assets class handles are still referenced by other objects, those references will still be active and will prevent garbage-collection of those assets. So when the constructor of Assets loads everything again, you will have two copies of each asset which is still referenced.

You can avoid that if all classes which use Assets never keep any references to assets around.

If you can't or don't want to avoid this for some reason, consider using at least a WeakReference. Weak references are ignored when it comes to determining what can be garbage-collected. A drawback of this is that you can not rely on a weak reference being valid. Always check the return value of weakReference.get() for null.

\$\endgroup\$

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.