0. Notice - earlier version

  • 要使用WebView不造成内存泄漏,首先应该做的就是不能在xml中定义webview节点,而是在需要的时候动态生成。即:可以在使用WebView的地方放置一个LinearLayout类似ViewGroup的节点,然后在要使用WebView的时候,动态生成即:
  1. WebView mWebView = new WebView(getApplicationgContext());
  2. LinearLayout mll = findViewById(R.id.xxx);
  3. mll.addView(mWebView);

然后一定要在onDestroy()方法中显式的调用:

  1. protected void onDestroy() {
  2. super.onDestroy();
  3. mWebView.removeAllViews();
  4. mWebView.destroy()
  5. }

注意: new WebView(getApplicationgContext()) ;必须传入ApplicationContext如果传入Activity的Context的话,对内存的引用会一直被保持着。有人用这个方法解决了当Activity被消除后依然保持引用的问题。但是你会发现,如果你需要在WebView中打开链接或者你打开的页面带有flash,获得你的WebView想弹出一个dialog,都会导致从ApplicationContext到ActivityContext的强制类型转换错误,从而导致你应用崩溃。

1. What leads to Memory leak

1.1 InnerClass

  1. public class InnerClassActivity extends Activity{
  2. private static Leak mLeak;
  3. class Leak {
  4. int a = 3;
  5. private Context mLeakContext;
  6. Leak(Context context) {
  7. mLeakContext = context;
  8. }
  9. }
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.test);
  14. mLeak = new Leak(this);
  15. Toast.makeText(this, "This is InnerClassActivity", Toast.LENGTH_SHORT).show();
  16. }
  17. }

1.2 Singleton

  1. public class Singleton {
  2. private static Singleton instance;
  3. private Context mContext1;
  4. private Singleton(Context context) {
  5. this.mContext1 = context;
  6. }
  7. public static Singleton getInstance(Context context) {
  8. if(instance == null) {
  9. synchronized (Singleton.class) {
  10. if (instance == null) {
  11. instance = new Singleton(context);
  12. }
  13. }
  14. }
  15. return instance;
  16. }
  17. }

1.3 webview - earlier version

  1. public class WebViewCreateActivity extends Activity{
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.webview_create);
  6. LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
  7. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  8. WebView webView = new WebView(this);
  9. webView.setLayoutParams(layoutParams);
  10. WebSettings webSettings = webView.getSettings();
  11. webSettings.setJavaScriptEnabled(true);
  12. webSettings.setDomStorageEnabled(true);
  13. webView.loadUrl("https://www.baidu.com/");
  14. ll.addView(webView);
  15. Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
  16. }
  17. }

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-存在的内存泄漏的更多相关文章

  1. Android WebView Memory Leak WebView内存泄漏

    在这次开发过程中,需要用到webview展示一些界面,但是加载的页面如果有很多图片就会发现内存占用暴涨,并且在退出该界面后,即使在包含该webview的Activity的destroy()方法中,使用 ...

  2. 安卓android WebView Memory Leak WebView内存泄漏

    Android WebView Memory Leak WebView内存泄漏 在这次开发过程中,需要用到webview展示一些界面,但是加载的页面如果有很多图片就会发现内存占用暴涨,并且在退出该界面 ...

  3. Android内存优化14 内存泄漏常见情况5 特殊对象造成的内存泄漏 WebView内存泄漏

    WebView造成内存泄露 关于WebView的内存泄露,因为WebView在加载网页后会长期占用内存而不能被释放,因此我们在Activity销毁后要调用它的destory()方法来销毁它以释放内存. ...

  4. Android性能优化之利用LeakCanary检测内存泄漏及解决办法

    前言: 最近公司C轮融资成功了,移动团队准备扩大一下,需要招聘Android开发工程师,陆陆续续面试了几位Android应聘者,面试过程中聊到性能优化中如何避免内存泄漏问题时,很少有人全面的回答上来. ...

  5. 【转】android 内存泄漏相关收藏博客。

    关于android内存泄漏的研究   博客建了几个月,都没有去写,一是因为当时换工作,然后又是新入职(你懂的,好好表现),比较忙:二是也因为自己没有写博客的习惯了.现在还算是比较稳定了,加上这个迭代基 ...

  6. 关于android内存泄漏的研究

    博客建了几个月,都没有去写,一是因为当时换工作,然后又是新入职(你懂的,好好表现),比较忙:二是也因为自己没有写博客的习惯了.现在还算是比较稳定了,加上这个迭代基本也快结束了,有点时间来写写博客.好了 ...

  7. Android开发之漫漫长途 番外篇——内存泄漏分析与解决

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  8. Android内存优化(三)避免可控的内存泄漏

    相关文章 Android性能优化系列 Java虚拟机系列 前言 内存泄漏向来都是内存优化的重点,它如同幽灵一般存于我们的应用当中,有时它不会现身,但一旦现身就会让你头疼不已.因此,如何避免.发现和解决 ...

  9. android 内存泄漏检测工具 LeakCanary 泄漏金丝雀

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 内存泄漏检测工具 android 内存泄漏检测工具 ======== 内存泄漏 就是  无用的对 ...

随机推荐

  1. [LeetCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  2. 动态生成验证码———MVC版

    上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...

  3. 【MySQL】mysql workbench

    1.导入,导出,数据之间的传输[两台服务器]2.连接管理3.服务器管理4.表的管理

  4. spring 拦截器

    1.mvc.xml <!-- 自定义拦截链配置 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping p ...

  5. JavaIO学习笔记(五)

    JavaIO前期准备 什么是同步 指的是用户进程触发IO操作并等待或者轮询的去查看IO操作是否就绪 什么是异步 异步是指用户进程触发IO操作以后便开始做自己的事情,而当IO操作已经完成的时候会得到IO ...

  6. Newick format tree

    1. all branches + leaf names + internal supports ((D:0.723274,F:0.567784)1.000000:0.067192,(B:0.2793 ...

  7. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  8. 个人CKeditor的config.js配置

    /** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For lic ...

  9. Unity Animator动画状态机 深入理解(一)

    接触Unity以来就已经有了Animator,Animation用的少,不过也大概理解他俩之间的一个区别于联系. 图中其实就是Animator和Animation之间的区别于联系了,啊!你肯定会告诉我 ...

  10. C#基础练习

    1.冒泡排序 namespace _0 { class Program { public static int[] BubbleSort(int[] arr) { ; i < arr.Lengt ...