在使用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. Don’t use Suspend and Resume, but don’t poll either.

    http://www.paradicesoftware.com/blog/2014/02/dont-use-suspend-and-resume-but-dont-poll-either/ Don’t ...

  2. 使用VS连接SQLServe时提示未能载入文件或程序集“System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKey

    解决方法:         就是去微软主页下载两个Microsoft SQL Server 2012补丁包,SQLSysClrTypes.msi和SharedManagementObjects.msi ...

  3. Xen安全架构sHype/ACM策略配置图文教程

    实验要求 1.     熟悉Xen虚拟化平台部署: 2.     Xen sHype/ACM安全架构中的Simple TE和Chinese Wall策略及事实上现机制的分析与验证. 第1章       ...

  4. 容器的end()方法

    容器的end()方法,返回一个迭代器,需要注意:这个迭代器不指向实际的元素,而是表示末端元素的下一个元素,这个迭代器起一个哨兵的作用,表示已经处理完所有的元素. 因此,在查找的时候,返回的迭代器,不等 ...

  5. Codeforces Round #308 (Div. 2) A. Vanya and Table 暴力

    A. Vanya and Table Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/552/pr ...

  6. 关于ORACLE DUAL表

    1.DUAL表的用途 Dual 是 Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中--查看当前连接用户 SQL> select user from d ...

  7. GLSL-几何着色器详解跟实例(GS:Geometry Shader)[转]

    [OpenGL4.0]GLSL-几何着色器详解和实例(GS:Geometry Shader) 一.什么是几何着色器(GS:Geometry Shader) Input Assembler(IA)从顶点 ...

  8. Java final修饰形参

    转自:http://java.chinaitlab.com/base/836044.html public class BB{ public int i; } public class PP{ pub ...

  9. ORCLE INNODB 博客与 innodb_lru_scan_depth

    https://blogs.oracle.com/mysqlinnodb/ http://mysqllover.com/?p=485 •MySQL. MySQL 5.6.10 http://www.m ...

  10. tar备份工具

    一.tar命令的基本格式 在UNIX.Linux系统中,有许多命令可以用于备份数据,其中最常见的命令是tar命令..tar是UNIX和Linux系统中的打包工具,可以将多个文件或目录打包(也称为归档) ...