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();
}
}
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.
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.
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.
Common causes for these memory leaks are: Excessive session objects. Insertion without deletion into Collection objects. Unbounded caches.
Set Editor.dialog to null in hideDialog() so that it can properly be garbage collected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With