In Android, we usually use Android's messaging mechanism for communication between threads, and this mechanism passes exactly the message.
In general, we use Message.obtain () and Handler.obtainmessage () to get a message from the message pool to avoid constructing the message directly.
- so does Android cause oom because the message pool caches the message object? for this question, I can clearly say that the app will not be oom because of the message pool. As for why, can take a step-by-step look down, impatient can directly see the last section--message pool how to store a Message.
Message obtain Analysis Message.obtain () source code
/** * Return a new Message instance F Rom 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 (); }
From the code slice, you can see that the message is assigned directly by spool.
Handler.obtainmessage () Source code
/** * 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. */ publicfinalobtainMessage() { return Message.obtain(this); }
Handler.obtain () is eventually called Message.obtain () to get it.
Message Pool related source code Analysis message Pool data structure
//Sometimes we store linked lists of these things /*package*/Message Next;Private Static FinalObject Spoolsync =NewObject ();Private StaticMessage SPool;Private Static intSpoolsize =0;Private Static Final intMax_pool_size = -;Private Static BooleanGcheckrecycle =true;
It is clear from the code that the data structure of the Message pool is actually a linked list. Spool is a global message pool, Spoolsize records the length of the linked list, max_pool_size indicates that the maximum length of the list is 50.
Message pool how to store message
/** @hide * / Public Static void updatecheckrecycle(intTargetsdkversion) {if(Targetsdkversion < Build.version_codes. LOLLIPOP) {gcheckrecycle =false; } }/** * Return A Message instance to the global pool. * <p> * You must don't touch the Message after calling this function because it has * effectively been freed. It is an error to recycle a message which is currently * enqueued or that's in the process of being delivered to a Ha Ndler. * </p> * * Public void Recycle() {if(Isinuse ()) {if(gcheckrecycle) {Throw NewIllegalStateException ("This message cannot is recycled because it"+"is still on use."); }return; } recycleunchecked (); }/** * Recycles a Message that is May in-use. * Used internally by the MessageQueue and Looper when disposing of queued Messages. */ voidRecycleunchecked () {//Mark the message as in and the 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++; } } }
From the code analysis, the core method of message pool storage is the recycleunchecked () method above:
1. Empty the Message object field to be reclaimed (avoid a static message pool memory leak due to the message being too large). therefore, regardless of how large the original message object is, and is eventually cached before the message pool is empty, the memory size of these cached message objects is basically negligible for an app memory. Therefore, the Message pool does not cause the app to Oom.
2. To determine whether the current thread pool size is less than 50, with the built-in lock pair method (thread safe). If it is less than 50, the mesaage is inserted directly to the tail of the message pool chain, and if it is greater than or equal to 50, the discarded message is disposed of by GC.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Message Pool What a ghost-source angle Analysis