引言
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()来获取的。那么我门再来看下Mess