在使用Android WebView的时候,可能会造成Activity的内存泄漏,这个是Android的Bug,目前发现在WebView内部在使用TintResources时会发生内存泄漏,但是在appcompat-v7:23.2.1中已经修复了这个问题。所以当发生WebView的Context内存泄漏时,如果泄漏引用是来自TickResources时,可以将appcompat-v7包换成23.2.1或以上就可以解决了。具体原因我们可以来下源码:

  appcompat-v7 23.2.0 中的TintResources:

 public class TintResources extends Resources {
private final Context mContext; public TintResources(@NonNull final Context context, @NonNull final Resources res) {
super(res.getAssets(), res.getDisplayMetrics(), res.getConfiguration());
mContext = context;
} /**
* We intercept this call so that we tint the result (if applicable). This is needed for
* things like {@link android.graphics.drawable.DrawableContainer}s which can retrieve
* their children via this method.
*/
@Override
public Drawable getDrawable(int id) throws NotFoundException {
return AppCompatDrawableManager.get().onDrawableLoadedFromResources(mContext, this, id);
} final Drawable superGetDrawable(int id) {
return super.getDrawable(id);
}
}

appcompat-v7 23.2.1中的TintResources:

 public class TintResources extends Resources {
private final WeakReference<Context> mContextRef; public TintResources(@NonNull final Context context, @NonNull final Resources res) {
super(res.getAssets(), res.getDisplayMetrics(), res.getConfiguration());
mContextRef = new WeakReference<>(context);
} /**
* We intercept this call so that we tint the result (if applicable). This is needed for
* things like {@link android.graphics.drawable.DrawableContainer}s which can retrieve
* their children via this method.
*/
@Override
public Drawable getDrawable(int id) throws NotFoundException {
final Context context = mContextRef.get();
if (context != null) {
return AppCompatDrawableManager.get().onDrawableLoadedFromResources(context, this, id);
} else {
return super.getDrawable(id);
}
} final Drawable superGetDrawable(int id) {
return super.getDrawable(id);
}
}

可以很清楚的看的23.2.1版本只是将Context的强引用改成了弱引用来避免内存泄漏。

Android TintResources Leak的更多相关文章

  1. Android Handler leak 分析及解决办法

    In Android, Handler classes should be static or leaks might occur, Messages enqueued on the applicat ...

  2. Android Handler Leak

    转自:Android中使用Handler引发的内存泄露 在Activity中,经常会用到自定义的Handler来处理主线程收到的Message,但是ADT20以后,直接定义的如下定义的内部会有提示说这 ...

  3. Android Memory Leak

    线程也是造成内存泄露的一个重要的源头.线程产生内存泄露的主要原因在于线程生命周期的不可控.1.看一下下面是否存在问题 public class ThreadActivity extends Activ ...

  4. Android 内存优化 (防Memory Leak)

      在之前的 Android 内存管理 &Memory Leak & OOM 分析 中,说到了Android的内存管理相关的原理,也能了解到Android Memory Leak 和 ...

  5. WelcomeActivity【欢迎界面】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 简单记录下欢迎界面的布局以及倒计时和跳过功能. 效果图 代码分析 1.修改APP整个主题为无标题栏样式:styles.xml文件 & ...

  6. Android 内存管理 &Memory Leak & OOM 分析

    转载博客:http://blog.csdn.net/vshuang/article/details/39647167 1.Android 进程管理&内存 Android主要应用在嵌入式设备当中 ...

  7. Android Memory/Resource Leak总结

    Android的内存/资源泄露,不容易发现,又会引发app甚至是system的一系列问题. 在这里我根据以往碰到的相关问题,总结出了一些检测和修改方法. *有可能造成memory leak的代码是Fr ...

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

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

  9. Android 内存管理 &amp;Memory Leak &amp; OOM 分析

    1.Android 流程管理&内存 Android主要应用在嵌入式设备其中.而嵌入式设备因为一些众所周知的条件限制,通常都不会有非常高的配置,特别是内存是比較有限的. 假设我们编写的代 码其中 ...

随机推荐

  1. IE兼容CSS3圆角border-radius的方法(同时兼容box-shadow,text-shadow)

    IE兼容CSS3圆角border-radius,box-shadow,text-shadow的方法 1.下载ie-css3.htc 2.CSS box { -moz-border-radius: 15 ...

  2. Mac下cocos2dx-3.0打包Android时,提示&quot;SimpleAudioEngine.h&quot;not found的解决方法

    前段时间触控公布cocos2dx-3.0,在升级之后试过之后,在最初的不习惯之后,感觉比之前的好用了不少,在下之前一直是用xCode模板创建,这回算是一口气升到顶了. 之后再一次编程时须要用到Sima ...

  3. IOS 7 Study - Implementing Navigation with UINavigationController

    ProblemYou would like to allow your users to move from one view controller to the other witha smooth ...

  4. Codeforces Good Bye 2015 C. New Year and Domino 前缀和

    C. New Year and Domino 题目连接: http://www.codeforces.com/contest/611/problem/C Description They say &q ...

  5. 目标检測的图像特征提取之(一)HOG特征

    1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检測的特征描写叙述子.它通过计算和统计图像局部区 ...

  6. android程序逆向工程

    随着智能手机的普及,功能越来越强大.程序也越来多和复杂化.研究一下android系统的逆向工程也是挺有意思的. 目前android逆向工程还处于初级阶段.表现在于: 1.没有完整的动态调试程序.目前由 ...

  7. SQL SERVER中查找某关键词位于哪些存储过程或函数

    USE [MYDB] go SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE routine_type='PROCEDURE' AND routine_d ...

  8. careercup-高等难度 18.1

    18.1  编写一个函数,将两个数字相加,不得使用+或其他算术运算符. int add(int a,int b) { ) return a; int sum=a^b; ; return add(sum ...

  9. Jquery实现文本框获取焦点清空内容,失去焦点重新获得内容的公共函数

    最近在做一个同城交友网站你给我吧(www.niyuewo.com)时写的一个函数与大家分享: //清除input中的内容 $(document).ready(function(){ $("i ...

  10. shell 学习笔记

    <Linux命令行与shell脚本编程大全>笔记   wkss 其他:http://www.cnblogs.com/pengdonglin137/p/3528303.html 一.基本命令 ...