標籤:proc more exception his exce lag ber after list
引言
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()方法:
- 將待回收的Message對象欄位置空(避免因Message過大。使靜態訊息池記憶體流失)。因此不管原先的Message對象有多大。終於被緩衝進Message Pool前都被置空,那麼這些緩衝的Message對象所佔記憶體大小對於一個app記憶體來說基本能夠忽略。所以說。Message Pool並不會造成App的OOM。
- 以內建鎖的方式(安全執行緒),判斷當前線程池的大小是否小於50。若小於50,直接將Mesaage插入到訊息池鏈表尾部;若大於等於50。則直接丟棄掉。那麼這些被丟棄的Message將交由GC處理。
總結
Android的Message Pool是個什麼鬼,Message Pool會否引起OOM——原始碼角度分析