WebView-存在的内存泄漏
0. Notice - earlier version
- 要使用WebView不造成内存泄漏,首先应该做的就是不能在xml中定义webview节点,而是在需要的时候动态生成。即:可以在使用WebView的地方放置一个LinearLayout类似ViewGroup的节点,然后在要使用WebView的时候,动态生成即:
WebView mWebView = new WebView(getApplicationgContext());LinearLayout mll = findViewById(R.id.xxx);mll.addView(mWebView);
然后一定要在onDestroy()方法中显式的调用:
protected void onDestroy() {super.onDestroy();mWebView.removeAllViews();mWebView.destroy()}
注意: new WebView(getApplicationgContext()) ;必须传入ApplicationContext如果传入Activity的Context的话,对内存的引用会一直被保持着。有人用这个方法解决了当Activity被消除后依然保持引用的问题。但是你会发现,如果你需要在WebView中打开链接或者你打开的页面带有flash,获得你的WebView想弹出一个dialog,都会导致从ApplicationContext到ActivityContext的强制类型转换错误,从而导致你应用崩溃。
1. What leads to Memory leak
1.1 InnerClass
public class InnerClassActivity extends Activity{private static Leak mLeak;class Leak {int a = 3;private Context mLeakContext;Leak(Context context) {mLeakContext = context;}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.test);mLeak = new Leak(this);Toast.makeText(this, "This is InnerClassActivity", Toast.LENGTH_SHORT).show();}}
1.2 Singleton
public class Singleton {private static Singleton instance;private Context mContext1;private Singleton(Context context) {this.mContext1 = context;}public static Singleton getInstance(Context context) {if(instance == null) {synchronized (Singleton.class) {if (instance == null) {instance = new Singleton(context);}}}return instance;}}
1.3 webview - earlier version
public class WebViewCreateActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.webview_create);LinearLayout ll = (LinearLayout) findViewById(R.id.ll);LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);WebView webView = new WebView(this);webView.setLayoutParams(layoutParams);WebSettings webSettings = webView.getSettings();webSettings.setJavaScriptEnabled(true);webSettings.setDomStorageEnabled(true);webView.loadUrl("https://www.baidu.com/");ll.addView(webView);Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();}}
2. How to detect the Memory Leak
2.1 Android Studio
- GC manually
- dump Java Heap

2.2 MemoryLeak in Our Project
| 品牌 | 固件 | 泄漏点 |
|---|---|---|
| 三星 | 4.0.4 | LightAppManager |
| 小米 | 5.0.1 | LightAppManager/ mAccessibilityManager |
| 华为 | 6.0 | LightAppManager/ mAccessibilityManager |
2.2.1 LightAppManager泄漏
3次手动GC后,内存的增长:

泄漏点1: LightAppManager 泄漏

泄漏点2: WebView-wrapper getSystemService泄漏

3. 官方态度
It's 2016 now and, as far as I can see it, the issue still hasn't been resolved. I tested it on Nexus 5 and Nexus 6 with the latest WebView updates (since the component is now separate from the OS itself). Could someone, please, take a look at this issue?!
来源: https://code.google.com/p/android/issues/detail?id=9375
WebView-存在的内存泄漏的更多相关文章
- Android WebView Memory Leak WebView内存泄漏
在这次开发过程中,需要用到webview展示一些界面,但是加载的页面如果有很多图片就会发现内存占用暴涨,并且在退出该界面后,即使在包含该webview的Activity的destroy()方法中,使用 ...
- 安卓android WebView Memory Leak WebView内存泄漏
Android WebView Memory Leak WebView内存泄漏 在这次开发过程中,需要用到webview展示一些界面,但是加载的页面如果有很多图片就会发现内存占用暴涨,并且在退出该界面 ...
- Android内存优化14 内存泄漏常见情况5 特殊对象造成的内存泄漏 WebView内存泄漏
WebView造成内存泄露 关于WebView的内存泄露,因为WebView在加载网页后会长期占用内存而不能被释放,因此我们在Activity销毁后要调用它的destory()方法来销毁它以释放内存. ...
- Android性能优化之利用LeakCanary检测内存泄漏及解决办法
前言: 最近公司C轮融资成功了,移动团队准备扩大一下,需要招聘Android开发工程师,陆陆续续面试了几位Android应聘者,面试过程中聊到性能优化中如何避免内存泄漏问题时,很少有人全面的回答上来. ...
- 【转】android 内存泄漏相关收藏博客。
关于android内存泄漏的研究 博客建了几个月,都没有去写,一是因为当时换工作,然后又是新入职(你懂的,好好表现),比较忙:二是也因为自己没有写博客的习惯了.现在还算是比较稳定了,加上这个迭代基 ...
- 关于android内存泄漏的研究
博客建了几个月,都没有去写,一是因为当时换工作,然后又是新入职(你懂的,好好表现),比较忙:二是也因为自己没有写博客的习惯了.现在还算是比较稳定了,加上这个迭代基本也快结束了,有点时间来写写博客.好了 ...
- Android开发之漫漫长途 番外篇——内存泄漏分析与解决
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android内存优化(三)避免可控的内存泄漏
相关文章 Android性能优化系列 Java虚拟机系列 前言 内存泄漏向来都是内存优化的重点,它如同幽灵一般存于我们的应用当中,有时它不会现身,但一旦现身就会让你头疼不已.因此,如何避免.发现和解决 ...
- android 内存泄漏检测工具 LeakCanary 泄漏金丝雀
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 内存泄漏检测工具 android 内存泄漏检测工具 ======== 内存泄漏 就是 无用的对 ...
随机推荐
- [LeetCode] Flip Game 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- EntityFramework、Dapper vs 草根框架性能大比拼,数据库访问哪家强?
扯淡 当前市面上 ORM 很多,有跑车型的,如 Dapper,有中规中矩型的,如 Mybatis.Net,也有重量型的,如 EntityFramework 和 NHibernate,还有一些出自草根之 ...
- jquery-三级联动
html <!DOCTYPE html> <html> <head> <meta charset=gbk /> <title>selectL ...
- 精灵方向移动问题[math.floor]
local xd = math.cos(math.rad(self._direction));--self._direction方向角度 local yd = math.sin(math.rad(se ...
- cocos2d-x 帧动画
ani = cc.Animation:create(); ...... local animate = cc.Animate:create(ani); s:runAction(animate); 发现 ...
- [转]mysql 视图
转载自http://blog.csdn.net/evankaka/article/details/47071133 一. 视图概述 视图是一个虚拟表,其内容由查询定义.带来许多好处: 1. 视图能简化 ...
- php实现文件上传与下载(上)
php实现文件的上传与下载是一个挺基本的功能,一般网站多多少少都会有这样的需求在内,当然不是说所有的文件都可以被上传,那这网络就太没有安全性可言了.因为接触php时间不长,今天写练练手,随笔也就是公开 ...
- phpcms首页实现轮播图
1.在你想要加轮播图的位置加入以下 <div id="flowDiagram" > <div id="button"> <span ...
- nginx本地转发
在conf文件下找到nginx.conf配置文件:添加如下代码:
- jquery1.7.2的源码分析(四)$.Deferred(2)
jQuery.Callbacks = function( flags ) { // Convert flags from String-formatted to Object-formatted // ...