引言

Android中,我们在线程之间通信传递通常採用Android的消息机制,而这机制传递的正是Message。

通常。我们使用Message.obtain()Handler.obtainMessage()从Message Pool中获取Message。避免直接构造Message。

  • 那么Android会否由于Message Pool缓存的Message对象而造成OOM呢?

对于这个问题,我能够明白的说APP***不会因Message Pool而OOM***。至于为什么,能够一步步往下看,心急的能够直接看最后一节——Message Pool怎样存放Message。

Obtain分析

Handler.obtainMessage()源代码

    /**
* Returns a new {@link android.os.Message Message} from the global message pool. More efficient than
* creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this).
* If you don't want that facility, just call Message.obtain() instead.
*/
public final Message obtainMessage()
{
return Message.obtain(this);
}

显然。Handler.obtain()是调用Message.obtain()来获取的。那么我门再来看下Message.obtain()源代码

    /**
* Return a new Message instance from the global pool. Allows us to
* avoid allocating new objects in many cases.
*/
public static Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
Message m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
sPoolSize--;
return m;
}
}
return new Message();
}

上述代码给我们透露几个个关键信息:

1. 学过一点数据结构的。从上面的代码片基本就能判断出sPool是一个链表结构。另外sPool本身就是Message

2. 若链表sPool不为空,那么obtain()方法会从链表sPool头部取出一个Message对象赋值给m,并作为返回值返回。否则。直接new一个Message对象。

剧透下这里的sPool事实上就是Message Pool

Message Pool相关源代码分析

Message Pool数据结构

public final class Message implements Parcelable {
// sometimes we store linked lists of these things
/*package*/ Message next; private static final Object sPoolSync = new Object();
private static Message sPool;
private static int sPoolSize = 0; private static final int MAX_POOL_SIZE = 50;
}

看到关键信息了没?Message的成员有next、sPool和sPoolSize。这对于略微学过一点数据结构的,非常快就能判断出这是一个典型的链表结构的实现。sPool就是一个全局的消息池即链表。next记录链表中的下一个元素,sPoolSize记录链表长度。MAX_POOL_SIZE表示链表的最大长度为50。

Message Pool怎样存放Message

public final class Message implements Parcelable {
private static boolean gCheckRecycle = true; /** @hide */
public static void updateCheckRecycle(int targetSdkVersion) {
if (targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
gCheckRecycle = false;
}
} /**
* Return a Message instance to the global pool.
* <p>
* You MUST NOT touch the Message after calling this function because it has
* effectively been freed. It is an error to recycle a message that is currently
* enqueued or that is in the process of being delivered to a Handler.
* </p>
*/
public void recycle() {
if (isInUse()) {
if (gCheckRecycle) {
throw new IllegalStateException("This message cannot be recycled because it "
+ "is still in use.");
}
return;
}
recycleUnchecked();
} /**
* Recycles a Message that may be in-use.
* Used internally by the MessageQueue and Looper when disposing of queued Messages.
*/
void recycleUnchecked() {
// Mark the message as in use while it remains in the recycled object pool.
// Clear out all other details.
flags = FLAG_IN_USE;
what = 0;
arg1 = 0;
arg2 = 0;
obj = null;
replyTo = null;
sendingUid = -1;
when = 0;
target = null;
callback = null;
data = null; synchronized (sPoolSync) {
if (sPoolSize < MAX_POOL_SIZE) {
next = sPool;
sPool = this;
sPoolSize++;
}
}
}
}

从代码分析上看。消息池存放的核心方法就是上面的recycleUnchecked()方法:

  1. 将待回收的Message对象字段置空(避免因Message过大。使静态的消息池内存泄漏)。因此不管原先的Message对象有多大。终于被缓存进Message Pool前都被置空,那么这些缓存的Message对象所占内存大小对于一个app内存来说基本能够忽略。所以说。Message Pool并不会造成App的OOM。

  2. 以内置锁的方式(线程安全),判断当前线程池的大小是否小于50。若小于50,直接将Mesaage插入到消息池链表尾部;若大于等于50。则直接丢弃掉。那么这些被丢弃的Message将交由GC处理。

总结

  • Message Pool是一个链表的数据结构。本身就是Message中的静态成员sPool(注。也是Message)

  • Message Pool不会由于缓存Message对象而造成OOM。

Android的Message Pool是个什么鬼,Message Pool会否引起OOM——源代码角度分析的更多相关文章

  1. Android的Message Pool是什么——源码角度分析

    原文地址: http://blog.csdn.net/xplee0576/article/details/46875555 Android中,我们在线程之间通信传递通常采用Android的消息机制,而 ...

  2. Android的消息处理机制,handler,message,looper(一)

    当应用程序启动时,Android首先会开启一个主线程(也就是UI线程),主线程为管理界面中的UI控件.在程序开发时,对于比较耗时的操作,通常会为其开辟一个单独的线程来执行,以尽可能减少用户的等待时间. ...

  3. Android Studio gradle编译 NullPointerException(no error message)解决

    原文:Android Studio gradle编译 NullPointerException(no error message)解决 1.关闭Android Studio  2.找到工程目录下的 . ...

  4. 异常:Message 850 not found; No message file for product=network, facility=NL解决方案

    一.异常信息:   Message 850 not found; No message file for product=network, facility=NL    二.解决方案:     后来在 ...

  5. 解决webApi<Message>An error has occurred.</Message>不能写多个Get方法的问题

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.     本人最近在研究C#webAPI相关知识,发现webAPI不能够支持 ...

  6. TNS-12560: Message 12560 not found; No message file for product=network, facility=TNS报错

    [oracle@localhost bin]$ ./lsnrctl startLSNRCTL for Linux: Version 12.2.0.1.0 - Production on 17-APR- ...

  7. EXP-00000: Message 0 not found; No message file for product=RDBMS, facility=EXP问题的解决方案

    EXP-00000: Message 0 not found; No message file for product=RDBMS, facility=EXP 最近在服务器上准备做一个批处理,定时备份 ...

  8. Message NNNN not found; No message file for product=network, facility=TNS

    Message NNNN not found; No message file for product=network, facility=TNS Table of Contents 1. 错误信息 ...

  9. Android异步消息处理机制完全解析,带你从源码的角度彻底理解(转)

    开始进入正题,我们都知道,Android UI是线程不安全的,如果在子线程中尝试进行UI操作,程序就有可能会崩溃.相信大家在日常的工作当中都会经常遇到这个问题,解决的方案应该也是早已烂熟于心,即创建一 ...

随机推荐

  1. python中判断字符串是否为中文

    判断字符串是否在中文编码范围内 for c in s:        if not ('\u4e00' <= c <= '\u9fa5'):            return False ...

  2. ntdsutil 清理弃用服务器-----待验证

    例子是这样的: 一个森林里有两个树,mm.com和cc.com,分别有dc www.mm.com和vdc.cc.com, cc.com域的控制器崩溃,不想恢复,要彻底删除这个域,由于vdc.cc.co ...

  3. 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

    在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...

  4. System.TypeInitializationException

    在连接数据库时,提示System.TypeInitializationException 数据库的连接字符串放在config文件中,而config中有两个文件,起初放在debug.comfig中,启动 ...

  5. 初学划分树,小见解之!POJ-2104/HDU-2665

    划分树 本来是学主席树的,可怜我等巨弱观群巨博客难解fotle主席的思想精髓.于是学了一下划分树,嗯,花了一下午时间理解build(其实自己模拟一遍就通了),我很难理解为什么划分树会看不懂而能学会主席 ...

  6. Android圆弧背景

    代码改变世界 Android圆弧背景 <?xml version="1.0" encoding="utf-8"?><shape xmlns:a ...

  7. [luoguP2221] [HAOI2012]高速公路(线段树)

    传送门 考虑每一段对答案的贡献 用每一段的左端点来表示当前这一段,那么区间就变成了[1,n-1] 如果询问区间[l,r],其中一个点的位置为x,则它对答案的贡献为(x-l)*(r-x)*s[x](s[ ...

  8. 洛谷P3760 - [TJOI2017]异或和

    Portal Description 给出一个\(n(n\leq10^5)\)的序列\(\{a_n\}(\Sigma a_i\leq10^6)\),求该数列所有连续和的异或和. Solution 线段 ...

  9. hdu 1166 树状数组(线段树)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. CodeForces 303B Rectangle Puzzle II

    题意: 给定一个靠着坐标轴长为n,宽为m的矩形和 矩形中的一个点A,求在这个矩形内部一个 长宽比为a/b的小矩形,使这个小矩形的长宽尽量大使点A在小矩形内部,并且点A尽量靠近小矩形的中心 CF的思维题 ...