2012-04-19 22 views
4

我們有活動和碎片泄漏並將原因追溯到TextView上的似乎未刪除的ChangeWatchers。TextView未刪除ChangeWatchers導致內存泄漏

場景: 活動A啓動活動B. B在其佈局中有一個textPassword EditText字段。活動B完成。

的HPROF轉儲顯示,仍然有活動B.的一個實例,其gcroot路徑如下:

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 

這也如果你Linkify.addLinks恰好一個TextView。

有沒有什麼辦法清理Activity B?

+1

你說「活動B完成」 - 它如何「完成」?你是否明確地調用'finish()',按下「BACK」按鈕或其他方法? – Squonk 2012-04-19 22:49:44

+0

test.maa.LoginActivity是B?您是否在傾銷HPROF之前強制使用GC,以確保它不是簡單地未收集到的?或者只是暫停,仍然參考請參閱@MisterSquonkq – zapl 2012-04-19 22:53:11

+0

在不同設備上進行進一步測試表明,運行3.1的GSlate上會出現此問題,但運行Thunderbolt時不會發生此問題。2.3.4 – Daddyboy 2012-04-19 23:04:35

回答

1

據我所知,這似乎是Android中與TextView ChangeWatcher和Linkify或Html.fromHtml spannable字符串有關的錯誤。我可以通過在我的活動的onDestroy()中調用setText(null)來解決該問題。可能還有其他解決方法也可以使用,但我無法找到有關泄漏的更多信息。

+1

我試過了,我似乎無法找到任何解決方法在它幾天。看到我的帖子以及http://stackoverflow.com/questions/18348049/android-edittext-memory-leak – MobDev 2013-08-21 04:03:53

-1

嘗試在onCreateView()中爲此特定視圖(包含任何android:textIsSelectable =「true」組件)使用Application Context而不是Activity Context。

// Singleton 
class MyApplication extends Application { 
    private static MyApplication mApp; 

    @Override 
    public void onCreate() { 
     mApp = this; 
    } 

    public static MyApplication getApp() { 
     return mApp; 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // Suggested inflater use Activity Context 
    // So we must tu use Application Context 
    Context context = MyApplication.getApp().getApplicationContext(); 
    LayoutInflater myLayoutInflater = LayoutInflater.from(context); 

    View view = myLayoutInflater.inflate(R.layout.my_view, container, false); 
    return view; 
}