Android的Message Pool是個什麽鬼,Message Pool會否引起OOM——源代碼角度分析
引言
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 [email protected] 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()方法:
- 將待回收的Message對象字段置空(避免因Message過大。使靜態的消息池內存泄漏)。因此不管原先的Message對象有多大。終於被緩存進Message Pool前都被置空,那麽這些緩存的Message對象所占內存大小對於一個app內存來說基本能夠忽略。所以說。Message Pool並不會造成App的OOM。
- 以內置鎖的方式(線程安全),判斷當前線程池的大小是否小於50。若小於50,直接將Mesaage插入到消息池鏈表尾部;若大於等於50。則直接丟棄掉。那麽這些被丟棄的Message將交由GC處理。
總結
Message Pool是一個鏈表的數據結構。本身就是Message中的靜態成員sPool(註。也是Message)
Message Pool不會由於緩存Message對象而造成OOM。
Android的Message Pool是個什麽鬼,Message Pool會否引起OOM——源代碼角度分析