1
\$\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\$
10
  • 1
    \$\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, 2017 at 5:45
  • 1
    \$\begingroup\$ So you can answer the question but just don't want too but instead would rather get my question taken down? I didn't want to add all that code but I am not sure how to properly ask my question without showing that code so others could really understand what I'm asking so they could give a helpful answer instead of just being confused. I certainly didn't intend to offend people on here to the point that they just want the question closed down without offering any insight on the actual question. \$\endgroup\$
    – ace106
    Jun 1, 2017 at 5:53
  • 1
    \$\begingroup\$ I guess we'll agree to disagree on whether or not it is related to game development. I think it does lean more to game development but you are entitled to your opinion and of course you can refuse to help. But thanks for commenting anyway and showing me that in the future I will try to be much more precise with the wording of my questions to avoid angering people and making them want a question closed down instead of providing help. \$\endgroup\$
    – ace106
    Jun 1, 2017 at 6:07
  • 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\$
    – user1430
    Jun 1, 2017 at 6:16
  • 1
    \$\begingroup\$ Thanks for the link. So If I ask my question as is, over at Stack Overflow, then I might get told that it should be posted here. Sadly I'm just not sure how to word my question better using proper technical terms for either site. I wrote the question that way to try and avoid confusing people or using a wrong term and angering people and instead it's backfired. \$\endgroup\$
    – ace106
    Jun 1, 2017 at 6:24

1 Answer 1

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\$

You must log in to answer this question.

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