7

我试图找出我的应用程序中的泄漏,但我不确定这是来自。

LeakCanary 告诉我我可以忽略它。那正确吗?

01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * LEAK CAN BE IGNORED.
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * com.mypackage.ui.map.MapComponentFragment has leaked:
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * GC ROOT android.view.inputmethod.InputMethodManager$1.this$0 (anonymous class extends com.android.internal.view.IInputMethodClient$Stub)
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references android.view.inputmethod.InputMethodManager.mCurRootView
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references com.android.internal.policy.impl.PhoneWindow$DecorView.mContext
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references com.mypackage.ui.MainActivity.mFragments
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references android.app.FragmentManagerImpl.mAdded
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references java.util.ArrayList.array
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * references array java.lang.Object[].[0]
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * leaks com.mypackage.ui.map.MapComponentFragment instance
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * Reference Key: 0790f013-1c87-4d5f-8c10-db277187e3ce
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * Device: samsung samsung SM-N910C treltexx
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * Android Version: 5.1.1 API: 22 LeakCanary: 1.4-SNAPSHOT 2714152
01-06 12:04:56.580 6935-9159/com.mypackage D/LeakCanary: * Durations: watch=5085ms, gc=149ms, heap dump=2562ms, analysis=10313ms

这个片段导致了几个泄漏,但我修复了它们。这个我不能让它消失。

有什么线索吗?

编辑

还有一件事,如果我想忽略它,这应该足够了吗?

.instanceField("android.view.inputmethod.InputMethodManager", "mCurRootView")

但仍然出现在 LeakCanary

显然,此泄漏在 AndroidExcludeRef 中,但仍在报告中。 https://github.com/square/leakcanary/issues/322

4

4 回答 4

3

是的,你可以忽略这个泄漏。

检查例如这个问题:https ://github.com/square/leakcanary/issues/256

于 2016-01-06T11:25:02.727 回答
3
/**
 * call this method in activity onDestroy() method.
*/
public static void fixInputMethod(Context context) {
    if (context == null) {
        return;
    }
    InputMethodManager inputMethodManager = null;
    try {
        inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    } catch (Throwable th) {
        th.printStackTrace();
    }
    if (inputMethodManager == null) {
        return;
    }
    Field[] declaredFields = inputMethodManager.getClass().getDeclaredFields();
    for (Field declaredField : declaredFields) {
        try {
            if (!declaredField.isAccessible()) {
                declaredField.setAccessible(true);
            }
            Object obj = declaredField.get(inputMethodManager);
            if (obj == null || !(obj instanceof View)) {
                continue;
            }
            View view = (View) obj;
            if (view.getContext() == context) {
                declaredField.set(inputMethodManager, null);
            } else {
                continue;
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }
}
于 2018-03-13T06:56:45.340 回答
2

是的,你可以忽略这个泄漏,但毕竟,泄漏是一个不受欢迎的场景,我们应该尝试使用任何黑客或技巧来修复它。

适用于所有版本的上述泄漏的优雅解决方案在这里

于 2017-03-04T07:22:51.683 回答
0

LeakCanary 已经将此泄漏视为 Android 操作系统故障。但仍然发生,这让我很恼火。如果有人遇到此问题并想要修复它,则有一种解决方法。

https://gist.github.com/pyricau/4df64341cc978a7de414

只需调用您的 Activity 的 onCreate 即可

IMMLeaks.fixFocusedViewLeak(getApplication());
于 2016-01-07T14:42:56.247 回答