Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView un-removed ChangeWatchers cause memory leak

Tags:

android

We have Activities and Fragments leaking and have traced the cause back to what appears to be un-removed ChangeWatchers on TextViews.

Scenario: Activity A starts Activity B. B has a textPassword EditText field in its layout. Activity B finishes.

The HPROF dump shows that there is still one instance of Activity B. Its gcroot path is the following:

test.maa.LoginActivity
'- mContext android.widget.EditText 
   '- this$0 android.widget.TextView$ChangeWatcher 
      '- [1] java.lang.Object[13] 
         '- mSpans android.text.SpannableStringBuilder 
            '- mSource android.text.method.PasswordTransformationMethod$PasswordCharSequence 
               '- mText android.text.MeasuredText 
                  '- mMeasured android.text.StaticLayout 
                     '- sStaticLayout class android.text.DynamicLayout 

This also happens if you Linkify.addLinks to a TextView.

Is there any way to clean up Activity B?

like image 251
Daddyboy Avatar asked Nov 25 '22 00:11

Daddyboy


1 Answers

As far as I can tell, this appears to be a bug in Android related to the TextView ChangeWatcher and Linkify or Html.fromHtml spannable strings. I was able to work around the problem by calling setText(null) in my activity's onDestroy(). There may be other workarounds that work as well, but I wasn't able to find any further information on the leak.

like image 136
emmby Avatar answered Jan 14 '23 21:01

emmby