LeakCanary发现Android WebView内存泄漏

LeakCanary发现Android WebView内存泄漏,第1张

概述我一直在使用工具 leakcanary来发现我的应用程序中的内存泄漏.看来我的WebViewActivity每次都会泄漏. 我创建了一个简单的应用程序来测试泄漏.我在xml布局文件中使用WebView启动一个Activity,并使用活动上下文进行膨胀. Js off.一切都默认期望一个简单的webViewClient在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();        }    }};@OverrIDepublic ResultReceiver getNewShowKeyboardReceiver() {    return mResultReceiver;}

使mResultReceiver成为私有的.

总结

以上是内存溢出为你收集整理的LeakCanary发现Android WebView内存泄漏全部内容,希望文章能够帮你解决LeakCanary发现Android WebView内存泄漏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1135593.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存