Mo Dev
Mo Dev

Reputation: 475

Android fragment manager memory leak

I use LeakCanary to detect memory leaks from my app. what is wrong with my code and why is this happening ? at the beginning I was refrencing to fragment manager as a new object, then I tried getSupportFragmentManager() but the same happens.

here is the log

    ```
ApplicationLeak(className=com.dev.myApp.ViewDialog, leakTrace=
┬
├─ android.app.ActivityThread
│    Leaking: NO (ActivityThread↓ is not leaking and a class is never leaking)
│    GC Root: System class
│    ↓ static ActivityThread.sCurrentActivityThread
├─ android.app.ActivityThread
│    Leaking: NO (ArrayMap↓ is not leaking)
│    ↓ ActivityThread.mActivities
├─ android.util.ArrayMap
│    Leaking: NO (Object[]↓ is not leaking)
│    ↓ ArrayMap.mArray
├─ java.lang.Object[]
│    Leaking: NO (ActivityThread$ActivityClientRecord↓ is not leaking)
│    ↓ array Object[].[3]
├─ android.app.ActivityThread$ActivityClientRecord
│    Leaking: NO (Editor↓ is not leaking)
│    ↓ ActivityThread$ActivityClientRecord.activity
├─ com.dev.myApp.Editor
│    Leaking: NO (Activity#mDestroyed is false)
│    ↓ Editor.dialog
│             ~~~~~~
╰→ com.dev.myApp.ViewDialog
​     Leaking: YES (Fragment#mFragmentManager is null and ObjectWatcher was watching this)
​     key = b2133c35-3af8-4631-81da-f73578e0dd12
​     watchDurationMillis = 62684
​     retainedDurationMillis = 57683
, retainedHeapByteSize=772214)```

and here is Java code

ViewDialog dialog;

public void showMyDialog(int position, String type){
    Bundle bundle = new Bundle();
    bundle.putInt(CONST.POSITION, position);
    bundle.putString(CONST.TYPE, type);
    dialog = new ViewDialog();
    dialog.setArguments(bundle);
    dialog.show(getSupportFragmentManager(), "apps_list");
}

public void hideDialog(){
    if (dialog != null) {
        dialog.dismiss();
    }
}

Upvotes: 8

Views: 2943

Answers (1)

Pierre-Yves Ricau
Pierre-Yves Ricau

Reputation: 8349

Set Editor.dialog to null in hideDialog() so that it can properly be garbage collected.

Upvotes: 2

Related Questions