2

我有内存泄漏,因为AppCompatTextView 它没有点击侦听器,它只是一个TexView带有一些文本的普通文本。

有什么我可以做的吗?这是一个错误还是我做错了什么?

在此处输入图像描述

我已经尝试过这里建议的解决方案,但没有帮助。

4

2 回答 2

2

这是一个android框架错误。https://code.google.com/p/android/issues/detail?id=34731 它尚未修复,即使在支持库中也是如此。

这是修复:

public static void fixInputMethodManagerLeak(Context destContext) {
    if (destContext == null) {
        return;
    }

    InputMethodManager imm = (InputMethodManager) destContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm == null) {
        return;
    }

    String[] arr = new String[]{"mCurRootView", "mServedView", "mNextServedView"};
    Field f = null;
    Object obj_get = null;
    for (int i = 0; i < arr.length; i++) {
        String param = arr[i];
        try {
            f = imm.getClass().getDeclaredField(param);
            if (!f.isAccessible()) {
                f.setAccessible(true);
            }
            obj_get = f.get(imm);
            if (obj_get != null && obj_get instanceof View) {
                View v_get = (View) obj_get;
                if (v_get.getContext() == destContext) {  // referenced context is held InputMethodManager want to destroy targets
                    f.set(imm, null);  // set empty, destroyed node path to gc
                } else {
                    // Not want to destroy the target, that is, again into another interface, do not deal with, to avoid affecting the original logic, there is nothing further for the cycle
                    Log.e(TAG, "fixInputMethodManagerLeak break, context is not suitable, get_context=" + v_get.getContext() + " dest_context=" + destContext);
                    break;
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

像这样称呼它:

@Override
protected void onDestroy() {
    super.onDestroy();
    //if you get memory leak on configuration change too, remove the if clause.
    if (isFinishing()) {
        fixInputMethodManagerLeak(this);
    }
}

也看看这个问题

于 2016-03-05T00:20:13.217 回答
0

根据这个链接: https ://code.google.com/p/android/issues/detail?id= 179272 看来泄漏是由于:

它发生在任何使用带有跨区文本的 TextLine(TextView、后代、布局)的东西上。由于 SearchView 在内部使用 SpannableStringBuilder,它会被泄露。

我希望它会帮助你:)

于 2016-03-04T22:24:59.963 回答