Chris Palma
Chris Palma

Reputation: 223

android - Activity still leaks even instance is set to null in onDestroy() method

I am stuck with memory leak in my main Activity (NavigationActivity)

public static NavigationActivity navigationActivity;

public static NavigationActivity getNavigationActivity() {
    return navigationActivity;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);

    navigationActivity = NavigationActivity.this;

}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (navigationActivity != null) {
        navigationActivity = null;       
    }      
}

Where does it leak? and how do I fix it? Any help would be appreciated. Thank you very much.

I'm using leak canary and this is the leak enter image description here

Upvotes: 3

Views: 1820

Answers (4)

Alex Misiulia
Alex Misiulia

Reputation: 1790

You cleared up the reference inside your activity. But class ContactsCompletionView still has strong reference to your activity. The best way to avoid this leak is not to expose link to your activity.
But if you really need this you can use WeakReference to your Activity. So your method getNavigationActivity() might looks like this:

public static WeakReference<NavigationActivity> navigationActivityRef;
{
    public static WeakReference<NavigationActivity> getNavigationActivity() {
      if(navigationActivityRef == null) {
          navigationActivityRef = new WeakReference<>(this);
      }
      return navigationActivityRef;
}     

Upvotes: 1

Vygintas B
Vygintas B

Reputation: 1694

You should set activity instance to null in onStop method, because onDestroy might not be called.

From Android developer documentation:

protected void onDestroy ()

Added in API level 1 Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

Upvotes: 1

J_Strauton
J_Strauton

Reputation: 2418

Make the instance a WeakReference.

private final WeakReference<Activity> navigationActivity;

Also onDestroy() is not guaranteed to be called. Read more on onDestroy() here.

Upvotes: 1

Dmitriy Puchkov
Dmitriy Puchkov

Reputation: 1601

try to use

@Override
protected void onStop() {
    super.onStop();
    navigationActivity = null;
}

but using static reference of Activity is very bad practise, don't do it.

In the most cases you can use activity.this reference or something like getActivity() method.

Upvotes: 1

Related Questions