1

LeakCanaryを使用してアプリのメモリ リークをチェックすると、次のようにリークが報告されます ここに画像の説明を入力

 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;
}

コードはTextView のsetEnable(true)addを呼び出し、既に に追加していますが、これでは十分ではありません。静的参照があり、.addTextChangedListenersetEnable(false)removeTextChangedListenerTextLine.sCachedsCashed

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ですか?

4

1 に答える 1

0

関連トピックAndroid のメモリ リークを textview で見つけました

Utils.clearTextLineCache() はおそらく良い回避策です。

于 2015-05-28T06:25:42.503 に答える