Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    }
}
like image 581
Mo Dev Avatar asked Aug 16 '19 09:08

Mo Dev


People also ask

How do you find memory leaks in Android applications?

The Memory Profiler is a component in the Android Profiler that helps you identify memory leaks and memory churn that can lead to stutter, freezes, and even app crashes. It shows a realtime graph of your app's memory use and lets you capture a heap dump, force garbage collections, and track memory allocations.

What causes memory leaks in Android?

Memory leaks occur when an application allocates memory for an object, but then fails to release the memory when the object is no longer being used. Over time, leaked memory accumulates and results in poor app performance and even crashes.

What are some best practices to avoid memory leaks on Android?

Causes of Memory Leaks and Their SolutionsOne should not use static views while developing the application, as static views are never destroyed. One should never use the Context as static, because that context will be available through the life of the application, and will not be restricted to the particular activity.

Which of the following actions can cause memory leak?

Common causes for these memory leaks are: Excessive session objects. Insertion without deletion into Collection objects. Unbounded caches.


1 Answers

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

like image 168
Pierre-Yves Ricau Avatar answered Nov 09 '22 09:11

Pierre-Yves Ricau