WebView内存泄漏,如何让WebView清除更彻底
来源:互联网 发布:mac 电源适配器 编辑:程序博客网 时间:2024/06/04 14:47
在这次开发过程中,需要用到webview展示一些界面,但是加载的页面如果有很多图片就会发现内存占用暴涨,并且在退出该界面后,即使在包含该webview的Activity的destroy()方法中,使用webview.destroy();webview=null;对内存占回收用还是没有任何效果。有人说,一旦在你的xml布局中引用了webview甚至没有使用过,都会阻碍重新进入Application之后对内存的gc。包括使用MapView有时一会引发OOM,几经周折在网上看到各种解决办法,在这里跟大家分享一下。但是到目前为止还没有找到根本的解决办法,网上也有说是sdk的bug。但是不管怎么样,我们还是需要使用的。
要使用WebView不造成内存泄漏,首先应该做的就是不能在xml中定义webview节点,而是在需要的时候动态生成。即:可以在使用WebView的地方放置一个LinearLayout类似ViewGroup的节点,然后在要使用WebView的时候,动态生成即:
1
WebView mWebView =
new
WebView(getApplicationgContext());
2
LinearLayout mll = findViewById(R.id.xxx);
3
mll.addView(mWebView);
1
protected
void
onDestroy() {
2
super
.onDestroy();
3
mWebView.removeAllViews();
4
mWebView.destroy()
5
}
于是大牛们就Activity销毁后还保持引用这个问题,提供了另一种解决办法:既然你不能给我删除引用,那么我就自己来吧。于是下面的这种方法诞生了:
(作者说这个方法是依赖android.webkit implementation有可能在最近的版本中失败)
01
public
void
setConfigCallback(WindowManager windowManager) {
02
try
{
03
Field field = WebView.
class
.getDeclaredField(
"mWebViewCore"
);
04
field = field.getType().getDeclaredField(
"mBrowserFrame"
);
05
field = field.getType().getDeclaredField(
"sConfigCallback"
);
06
field.setAccessible(
true
);
07
Object configCallback = field.get(
null
);
08
09
if
(
null
== configCallback) {
10
return
;
11
}
12
13
field = field.getType().getDeclaredField(
"mWindowManager"
);
14
field.setAccessible(
true
);
15
field.set(configCallback, windowManager);
16
}
catch
(Exception e) {
17
}
18
}
1
public
void
onCreate(Bundle savedInstanceState) {
2
super
.onCreate(savedInstanceState);
3
setConfigCallback((WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
4
}
5
6
public
void
onDestroy() {
7
setConfigCallback(
null
);
8
super
.onDestroy();
9
}
该反射方法在我的实验中(2.3.6)确实有些用处,在应用内存占用到70M左右的时候会明显释放到50M或者60M然后的释放就有些缓慢,其实就是看不出来了。之前在没使用该方法的时候可能达到120M。
但是!!!我们的应用要求占用内存更低啊,这肿么拌?凉拌么?No。在各种纠结之后,终于找到了终极解决办法!!!该办法适用于我们的需求,在退出WebView的界面之后,迅速回收内存。要问这个方法是什么,不要9999,不要8999,只要你仔细看好下面一句话:那就是为加载WebView的界面开启新进程,在该页面退出之后关闭这个进程。
这一点说了之后,你懂了吧?
但是在这个其中,杀死自己进程的时候又遇到了问题,网上介绍的各种方法都不好使,
killBackgroundProcesses(getPackageName());各种不好用,最后使用System.exit(0);直接退出虚拟机(Android为每一个进程创建一个虚拟机的)。这个肯定不用纠结了,一旦退出,内存里面释放。听涛哥说QQ也是这么做。
最后英雄要问出处,附上大牛解说引起该问题的出处
这个泄漏出现在external/webkit/Source/WebKit/android/WebCoreSupport/UrlInterceptResponse.cpp.中。具体我自己真心没有深入研究。大家有兴趣的话,可以看看哈。
--- a/Source/WebKit/android/WebCoreSupport/UrlInterceptResponse.cpp+++ b/Source/WebKit/android/WebCoreSupport/UrlInterceptResponse.cpp@@ -63,10 +63,10 @@ public: JNIEnv* env = JSC::Bindings::getJNIEnv(); // Initialize our read buffer to the capacity of out. if (!m_buffer) {- m_buffer = env->NewByteArray(out->capacity());- m_buffer = (jbyteArray) env->NewGlobalRef(m_buffer);+ ScopedLocalRef<jbyteArray> buffer_local(env, env->NewByteArray(out->capacity()));+ m_buffer = static_cast<jbyteArray>(env->NewGlobalRef(buffer_local.get())); } int size = (int) env->CallIntMethod(m_inputStream, m_read, m_buffer); if (checkException(env) || size < 0) return; // Copy from m_buffer to out.
而且从这里https://github.com/android/platform_external_webkit/commit/1e3e46a731730c02d916ea805ec4b20191509282这个bug的解决状态。
还有一个问题要说的,也是在WebView使用的时候出现的问题:WebView中包含一个ZoomButtonsController,当使用web.getSettings().setBuiltInZoomControls(true);启用该设置后,用户一旦触摸屏幕,就会出现缩放控制图标。这个图标过上几秒会自动消失,但在3.0系统以上上,如果图标自动消失前退出当前Activity的话,就会发生ZoomButton找不到依附的Window而造成程序崩溃,解决办法很简单就是在Activity的ondestory方法中调用web.setVisibility(View.GONE);方法,手动将其隐藏,就不会崩溃了。在3.0一下系统上不会出现该崩溃问题,真是各种崩溃,防不胜防啊!
最后还有内存泄漏的一些个建议:
In summary, to avoid context-related memory leaks, remember the following:
- Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
- Try using the context-application instead of a context-activity
- Avoid non-static inner classes in an activity if you don’t control their life cycle, use a static inner class and make a weak reference to the activity inside
And remember that a garbage collector is not an insurance against memory leaks. Last but not least, we try to make such leaks harder to make happen whenever we can.
- WebView内存泄漏,如何让WebView清除更彻底
- WebView内存泄漏,如何让WebView清除更彻底
- WebView内存泄漏,如何让WebView清除更彻底
- WebView内存泄漏,如何让WebView清除更彻底
- 如何让Android WebView访问更快
- webview内存泄漏问题
- WebView内存泄漏
- WebView内存泄漏记录
- Android WebView 内存泄漏
- WebView内存泄漏解决方法
- WebView内存泄漏解决方法
- WebView内存泄漏修复
- WebView内存泄漏--解决方法
- Android WebView Memory Leak WebView内存泄漏
- Android WebView Memory Leak WebView内存泄漏
- Android WebView Memory Leak WebView内存泄漏
- Android WebView Memory Leak WebView内存泄漏
- Android WebView Memory Leak WebView内存泄漏
- 2017 年最受欢迎的 10个编程挑战网站
- h5+js生成随机大小的雪花
- Android架构师或者独立开发,前期搭框架应该做哪些事情
- 【SMS】SMS协议介绍之SMS控制层(Control Layer)
- 解决子线程不共有父线程的请求上下文环境
- WebView内存泄漏,如何让WebView清除更彻底
- 请求发送者与接收者解耦——命令模式(三)
- 25岁社招进阿里,从电商到有赞新零售,仅1年就打开了马云一直想做的新领域!
- Error:java: Compilation failed: internal java compiler error
- jqGrid 翻页行保持选中
- Java RMI详解
- Object Detection系列(三) Fast R-CNN
- ArrayList和LinkList的区别
- springboot druid 数据库批量更新错误 multi-statement not allow