Question

When I was looking up the functionality of Handler.obtainMessage(), I read that it retrieves a message from global message pool instead of creating a new instance of message. Where does this global message pool reside?
Is it a collection of all the message objects that were created before?

Was it helpful?

Solution

It simply means that dalvik stores the messages in a pool of recycled objects. These are created and stored from a static pool thread:

private static Message sPool;

You won't have access directly to it, but you can still call methods such as recyle.

You can have a look at the source code here.

As for where exactly : the heap.

OTHER TIPS

It's a linked list in Message. The head of the list is stored in a static variable. Source.

Messages are put to the pool with recycle().

You can look into sources:

Message obtain() :

http://androidxref.com/4.4.2_r1/xref/frameworks/base/core/java/android/os/Message.java#106

which is using:

private static Message sPool;

a static class variable (actually it is a single linked list), so this pool is global to your application.

And in provided link, you can find public void recycle() method that returns no longer neede message to above pool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top