引言

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。

最新文章

  1. Google-glog 日志库使用手记
  2. 初识reactJs 相关
  3. [MetaHook] BaseUI hook
  4. OpenAl编程入门:播放一段音频
  5. Ajax无刷新提交
  6. 正则匹配ab不匹配aab
  7. IE filter &amp; z-index bug
  8. 转:更改 centos yum 源
  9. php 函数之 )_each()list()implode()explode()in_array()
  10. ubuntu12.04安装搜狗输入法配置,安装packettracer字体设置,软件推荐
  11. 1787: [Ahoi2008]Meet 紧急集合
  12. UE4 内容示例网络同步Learn
  13. android 实现倒影
  14. BZOJ_[usaco2007 Nov]relays 奶牛接力跑_离散化+倍增弗洛伊德
  15. [CC-MCO16306]Fluffy and Alternating Subsequence
  16. The Topo to Raster tool returns errors 010235 and 010067转
  17. #400 – 使用ItemsPanel 属性将WrapPanel 作为ListBox的显示面板(Using a WrapPanel as the Items Panel for a ListBox)
  18. error: this &#39;if&#39; clause does not guard... [-Werror=misleading-indentation]
  19. 建造者模式 build
  20. vue-cli脚手架之其他文件解释

热门文章

  1. arrive 和reach 的区别
  2. Python Flask+Mysql练习题
  3. GDB使用例子
  4. 此坑待填 离散化思想和凸包 UVA - 10173 Smallest Bounding Rectangle
  5. 九度oj 题目1447:最短路
  6. [UOJ#220][BZOJ4651][Noi2016]网格
  7. 干货 | Elasticsearch Nested类型深入详解
  8. 刷题总结——赛车(bzoj3190)
  9. 远程管理 KVM 虚机
  10. 树状数组求第K大(From CLJ)