Do listeners experience memory leaks if they are not removed from the ruined action?

If you add a listener to the / view control and do not delete it, will this create a memory leak? For example, onCreateadds a listener to EditTextwho listens for changes. Do you need to remove this listener in onDestroy?

I assume that if you use an anonymous listener or a local variable that implements a listener, memory will be free if it Activityis destroyed due to scoping rules.

The only way I could see the potential of a memory leak was to have the listener passed to the intent object. Thoughts?

+5
source share
2 answers

In fact, the listener does not create a memory leak. However, they are often used improperly and can lead to leaks. Sometimes you see code in which an object refers to a component (for example, to display messages there), which has a listener that references (possibly indirectly) to the first object. This forms a cycle, and all its members live and die together. When a component is a dialogue that should be short-lived, you may have a problem. Beginners tend to use objects like

class MyKitchenSink implements Runnable, KeyListener, ....

which can have many links and simplifies the collection of memory leak * . Not creating "universal classes" is the way to go.


* "" , C, , . , , , .

+8

, -, /, - onDestroy...

+2

All Articles