在使用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. UVa11205 The Broken Pedometer

    // 题意:有P个LED灯,以及N个字符,要求选出个数最少的LED灯,使得即使只有这些灯正常工作,也能区分出这N个字符 // 题意抽象:输入两个整数P, N以及N行P列的01矩阵,找少的列,能区分所有 ...

  2. Django 1.6 最佳实践: 如何设置django项目的设置(settings.py)和部署文件(requirements.txt)

    Django 1.6 最佳实践: 如何设置django项目的设置(settings.py)和部署文件(requirements.txt) 作者: Desmond Chen,发布日期: 2014-05- ...

  3. Java学习笔记之==与equals

    一.问题引入 Java测试两个变量是否相等有两种方式:==运算符和equals方法. 但是这二者完全一样吗?考虑下面程序: public class TestEqual { public static ...

  4. Navicat 导入数据报错 --- 1153 - Got a packet bigger than 'max_allowed_packet' bytes

    在用Navicat导入SQL文件时报错:MySql 错误 Err [Imp] 1153 - Got a packet bigger than 'max_allowed_packet' bytes 查了 ...

  5. jquery Mobile点击显示加载等待效果

    点击某个按钮或链接时,触发等待加载效果: <script> <!-- $(document).bind("mobileinit", function(){ }); ...

  6. Codeforces Round #335 (Div. 2) A. Magic Spheres 水题

    A. Magic Spheres Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606/ ...

  7. Golang学习 - sort 包

    ------------------------------------------------------------ // 满足 Interface 接口的类型可以被本包的函数进行排序. type ...

  8. The Socket API, Part 4: Datagrams

    转:http://www.linuxforu.com/2011/11/socket-api-part-4-datagrams/ By Pankaj Tanwar on November 1, 2011 ...

  9. Collections.sort(List<T> Comparator) 自定义排序

    Collections.sort(basicinfoList, new Comparator<MlisBasicinfo>() { @Override public int compare ...

  10. solr中通过SFTP访问文件建立索引

    需求: 从oracle数据库中根据记录的文件名filename_html(多个文件以逗号隔开),文件路径path,备用文件名bakpath中获取 主机172.21.0.31上对应的html文件内容,并 ...