2015-05-22 109 views
1

我用LeakCanary來檢查我的應用程序的內存泄漏, 並報告泄漏如下 enter image description here的EditText addTextChangedListener導致內存泄漏

public AutofitHelper setEnabled(boolean enabled) { 
    if (mEnabled != enabled) { 
     mEnabled = enabled; 

     if (enabled) { 
      mTextView.addTextChangedListener(mTextWatcher); 
      mTextView.addOnLayoutChangeListener(mOnLayoutChangeListener); 

      autofit(); 
     } else { 
     android.util.Log.i("linlian","AutofitHelper.setEnabled()remove="+mTextView); 
      mTextView.removeTextChangedListener(mTextWatcher); 
      mTextView.removeOnLayoutChangeListener(mOnLayoutChangeListener); 

      mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); 
     } 
    } 
    return this; 
} 

代碼調用setEnable(true)添加addTextChangedListener爲TextView的 ,我已經加上setEnable(false)removeTextChangedListener,但是 這還不夠,還有一個靜態的TextLine.sCached參考,如何發佈sCashed

後續的代碼片段我在一個TextLine

static TextLine recycle(TextLine tl) { 
    tl.mText = null; 
    tl.mPaint = null; 
    tl.mDirections = null; 

    tl.mMetricAffectingSpanSpanSet.recycle(); 
    tl.mCharacterStyleSpanSet.recycle(); 
    tl.mReplacementSpanSpanSet.recycle(); 

    synchronized(sCached) { 
     for (int i = 0; i < sCached.length; ++i) { 
      if (sCached[i] == null) { 
       sCached[i] = tl; 
       break; 
      } 
     } 
    } 
    return null; 
} 

發現,但是,如何使用它在正確的方式來回收靜態sCashed

回答