LeakCanary发现Android WebView内存泄漏

前端之家收集整理的这篇文章主要介绍了LeakCanary发现Android WebView内存泄漏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在使用工具 leakcanary来发现我的应用程序中的内存泄漏.看来我的WebViewActivity每次都会泄漏.

我创建了一个简单的应用程序来测试泄漏.我在xml布局文件中使用WebView启动一个Activity,并使用活动上下文进行膨胀. Js off.一切都默认期望一个简单的webViewClient在WebView中保持重定向.它每次泄漏.

我做了大量的研究,防止这种情况的唯一方法是使用另一个进程启动WebViewActivity并在onDestroy中杀死它.但这种方法有其自身的缺点.

每次运行5.0的所有设备都会发生泄漏,未检查4.3及以下版本.

泄漏信息粘贴在下面:

In com.example.webviewmemoryleaktest:1.0:1.
* com.example.webviewmemoryleaktest.WebViewActivity has leaked:
* GC ROOT android.os.ResultReceiver$MyResultReceiver.this$0
* references org.chromium.content.browser.ContentViewCore$2$1.this$1 (anonymous class extends android.os.ResultReceiver)
* references org.chromium.content.browser.ContentViewCore$2.this$0
* references org.chromium.content.browser.ContentViewCore.mContext
* references com.android.webview.chromium.ResourcesContextWrapperFactory$WebViewContextWrapper.mBase
* leaks com.example.webviewmemoryleaktest.WebViewActivity instance

  • Reference Key: 9a0346cf-6ad9-4b07-9329-a975d8fa3cbe
  • Device: LGE google Nexus 4 occam
  • Android Version: 5.1 API: 22
  • Durations: watch=5139ms,gc=188ms,heap dump=2822ms,analysis=30918ms

感谢是否有人可以提供帮助.谢谢!

解决方法

private ResultReceiver mResultReceiver = new ResultReceiver(new Handler()) {
    @Override
    public void onReceiveResult(int resultCode,Bundle resultData) {
        getContentViewClient().onImeStateChangeRequested(
                resultCode == InputMethodManager.RESULT_SHOWN
                || resultCode == InputMethodManager.RESULT_UNCHANGED_SHOWN);
        if (resultCode == InputMethodManager.RESULT_SHOWN) {
            // If OSK is newly shown,delay the form focus until
            // the onSizeChanged (in order to adjust relative to the
            // new size).
            // TODO(jdduke): We should not assume that onSizeChanged will
            // always be called,crbug.com/294908.
            getContainerView().getWindowVisibleDisplayFrame(
                    mFocusPreOSKViewportRect);
        } else if (hasFocus() && resultCode
                == InputMethodManager.RESULT_UNCHANGED_SHOWN) {
            // If the OSK was already there,focus the form immediately.
            assert mWebContents != null;
            mWebContents.scrollFocusedEditableNodeIntoView();
        }
    }
};

@Override
public ResultReceiver getNewShowKeyboardReceiver() {
    return mResultReceiver;
}

使mResultReceiver成为私有的.

原文链接:https://www.f2er.com/android/315060.html

猜你在找的Android相关文章