2015-05-22 75 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

回答