Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText Memory Leak

Alot of people are noticing EditText in an activity is holding a Strong Reference to an Activity even once its finished. To be clear this EditText is inside a layout and inflated, there is no Listeners set. This only happens on certain devices e.g. Samsung Galaxy S4 (Android 4.2.2) and others. Many post about this still no solution. First here is some useful posts. (Eventually GC will clean this so its not technically a leak, but for heavy memory apps it takes way to long and will cause OOM)

Android Samsung Memory leak in EditText

Why does EditText retain its Activity's Context in Ice Cream Sandwich

EditText causing memory leak

Possibility of unhandled memory leak

The solutions noted do not work for all devices. It comes down to the Edittext Watcher. I think there may be solution in overriding this Watcher then having a function to clean it up onDestroy(). Please any help here, I been at this for days.

Here is the MAT Histogram

Memory Leak

like image 612
MobDev Avatar asked Aug 21 '13 02:08

MobDev


1 Answers

It is because EditText references the context of Activity. When the Activity is destroyed, Activity cannot be recycled normally because Edittext holds a reference to the context of activity. Solution: Rewrite EditText, change the reference to the Context in Activity to the reference to ApplicationContext.

Instructions:

https://programming.vip/docs/solve-the-memory-leak-problem-caused-by-edittext-in-android.html

like image 166
Pablo Alfonso Avatar answered Oct 07 '22 12:10

Pablo Alfonso