DialogFragment memory leak error

In the application I'm working on, I see a memory leak in the DialogFragment dialog box, and the only way to fix it so far has been to delete all the views when the DialogFragment is destroyed, is this a normal thing that should do? I work with custom ROM, so I was not sure, maybe this is due to this problem. Is there a reason why DO NOT remove all views from the dialog can cause a memory leak?

@Override public void onDestroyView() { if (getView() instanceof ViewGroup) { ((ViewGroup)getView()).removeAllViews(); } super.onDestroyView(); } 
+5
source share
2 answers

This happens with my application, and I found a leak using Leakcanary.

This happens when you have a dialog with EditText. A cursor macro that is implemented using a callback is not handled properly when closing a view containing EditText. And this happens by chance.

https://code.google.com/p/android/issues/detail?id=188551

Edit

And here is what I do before each dialog.dismiss() :

 editText.setCursorVisible(false); dismiss(); 

Hope this helps!

+5
source

MemoryLeak can occur for many reasons, some common reasons:

  • Keep a reference to your object (in this case, an instance of your dialog box) in some static fields .
  • Pass Context to Thread or AsyncTask , because Threads are also GC root strong>!
  • Your class has a non-static inner class, in which case a memory leak occurs if the inner class is associated with GC root (for example, if the inner class is an AsyncTask instance).

In your case, perhaps you have a view related to GC root, while this view cannot be garbage collected, your dialog box in which the views are saved cannot be garbage collected either.

+4
source

Source: https://habr.com/ru/post/1247877/


All Articles