The potential memory leak depends only on your architecture.
This is usually normal so as not to call removeOnGlobalLayoutListener(myListener) . View contains a link to ViewTreeObserver , which contains a link to the added OnGlobalLayoutListener . Unless you have another reference to the listener, this is garbage collected on presentation.
Now, if your OnGlobalLayoutListener implementation contains a reference to the view, it is still beautiful. The reverse loop is not a problem for the Android garbage collector.
The problem can be created if you have another component that contains a reference to the OnGlobalLayoutListener implementation. If the component lives longer than the view (for example, it is held through the application object), you create a memory leak from the view (and context) through the listener.
It’s important not to hold the view when it is no longer in use. An easy way to avoid eye leak is to use WeakReference .
source share