WebView 中的内存泄漏 [英] Memory leak in WebView

查看:34
本文介绍了WebView 中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 xml 布局的活动,其中嵌入了 WebView.我根本没有在我的活动代码中使用 WebView,它所做的只是坐在我的 xml 布局中并且是可见的.

I have an activity using an xml layout where a WebView is embedded. I am not using the WebView in my activity code at all, all it does is sitting there in my xml layout and being visible.

现在,当我完成活动时,我发现我的活动没有从内存中清除.(我通过 hprof 转储检查).如果我从 xml 布局中删除 WebView,则该活动将完全清除.

Now, when I finish the activity, I find that my activity is not being cleared from memory. (I check via hprof dump). The activity is entirely cleared though if I remove the WebView from the xml layout.

我已经试过了

webView.destroy();
webView = null;

在我的活动的 onDestroy() 中,但这并没有多大帮助.

in onDestroy() of my activity, but that doesn't help much.

在我的 hprof 转储中,我的活动(名为浏览器")具有以下剩余的 GC 根(在对其调用 destroy() 之后):

In my hprof dump, my activity (named 'Browser') has the following remaining GC roots (after having called destroy() on it):

com.myapp.android.activity.browser.Browser
  - mContext of android.webkit.JWebCoreJavaBridge
    - sJavaBridge of android.webkit.BrowserFrame [Class]
  - mContext of android.webkit.PluginManager
    - mInstance of android.webkit.PluginManager [Class]  

我发现另一位开发者也遇到过类似的事情,参见Filipe Abrantes的回复:http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-android/

I found that another developer has experienced similar thing, see the reply of Filipe Abrantes on: http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-android/

确实是一个非常有趣的帖子.最近我过得很艰难对我的内存泄漏进行故障排除安卓应用.最终结果是我的 xml 布局包含一个 WebView组件,即使没有使用,也是防止记忆被g-在屏幕旋转/应用程序后收集重启……这是当前的bug吗实施,或者有什么具体什么时候需要做使用网络视图

Indeed a very interesting post. Recently I had a very hard time troubleshooting a memory leak on my Android app. In the end it turned out that my xml layout included a WebView component that, even if not used, was preventing the memory from being g-collected after screen rotations/app restart… is this a bug of the current implementation, or is there something specific that one needs to do when using WebViews

现在,不幸的是,博客或邮件列表上还没有关于这个问题的回复.因此我想知道,这是 SDK 中的一个错误(可能类似于报告的 MapView 错误 http://code.google.com/p/android/issues/detail?id=2181) 或如何通过嵌入的 webview 将活动完全脱离内存?

Now, unfortunately there has been no reply on the blog or the mailing list about this question yet. Therefore I am wondering, is that a bug in the SDK (maybe similar to the MapView bug as reported http://code.google.com/p/android/issues/detail?id=2181) or how to get the activity entirely off the memory with a webview embedded?

推荐答案

我从上面的评论和进一步的测试中得出结论,问题是 SDK 中的一个错误:当通过 XML 布局创建 WebView 时,Activity 作为WebView 的上下文,而不是应用程序上下文.完成活动时,WebView 仍然保留对活动的引用,因此活动不会从内存中删除.我为此提交了错误报告,请参阅上面评论中的链接.

I conclude from above comments and further tests, that the problem is a bug in the SDK: when creating a WebView via XML layout, the activity is passed as the context for the WebView, not the application context. When finishing the activity, the WebView still keeps references to the activity, therefore the activity doesn't get removed from the memory. I filed a bug report for that , see the link in the comment above.

webView = new WebView(getApplicationContext());

请注意,此解决方法仅适用于某些用例,即如果您只需要在 web 视图中显示 html,而无需任何 href 链接或对话框链接等.请参阅下面的评论.

这篇关于WebView 中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆