Note that there are some explanatory texts on larger screens.

plurals
  1. POClass member of a class is garbage collected even though the Application has a reference to it
    text
    copied!<p>I'm seeing a behavior that I wasn't expecting regarding garbage collection on Android.</p> <p>I have a class that extends Application named App and modified the manifest so that</p> <p>&lt;application android:name=".App" ...</p> <p>I've used this before to be able to get the Application context through a static method for convenience but also store small but crucial information that should only be garbage collected when the entire application is. This information is stored in a singleton object that is initialized and a reference stored in the application class:</p> <pre><code>public class App extends Application { Context mContext; Info info; @Override public void onCreate() { super.onCreate(); mContext = this; info = Info.getInstance(); this.setTheme(R.style.MyTheme); } public static Context getContext(){ return mContext; } } </code></pre> <p>When Info is initialized, a simple member object is initialized as well:</p> <pre><code>public class Info { public Profile profile; (...) private Info() { profile = new Profile(); } (...) } </code></pre> <p>The issue that I've experienced, when the App is paused and I run another memory intensive app (to force garbage collection), when I return to the App, I get a NullPointerException when trying to access the profile var </p> <p>Info.getInstance().profile</p> <p>Shouldn't the fact that the Application holds a reference to the "Info" singleton and the fact that the singleton holds a reference to "profile" keep it from getting garbage collected?</p> <p>Any help is appreciated. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload