Google I/O: Memory Management for Android Apps
May 16, 2011 ⋅ 5 Comments »Though I never made a formal announcement here, some of you may know that I joined the Android team at Google just over a year ago. Last week I had the privilege of speaking at Google I/O in a talk called “Memory Management for Android Applications”:
Android apps have more memory available to them than ever before, but are you sure you’re using it wisely? This talk will cover the memory management changes in Gingerbread and Honeycomb (concurrent GC, heap-allocated bitmaps, “largeHeap” option) and explore tools and techniques for profiling the memory usage of Android apps.
You can watch a full video of the talk (including the Q&A) on YouTube:
You can also download the slides (PDF).
ERRATUM: I’d like to correct one mistake I made in the Q&A, answering a question at 49:33 in the video. I said that “garbage collection is fundamentally kind of asynchronous; if you’re really close to the limit there can be times that you’re trying to allocate so fast that the garbage collector can’t keep up.” That isn’t really true. Dalvik will always perform a full, synchronous GC before thowing an OutOfMemoryError. What I was referring to here was finalization. Finalization is asynchronous, so if you are close to your heap limit and allocating a lot of objects, you may run out of memory before finalizable objects can be freed.
Anyways, I had a great time at Google I/O, and met all kinds of developers building cool stuff on Android. Hope to see you all again next year!
Kshitij Aggarwal - May 17, 2011:
Hi Patrick,
Firstly thankyou so much for the awesome session. Got to know a lot of new things specially about the pre-honeycomb bitmap storage in the unmanaged heap.
I have one small question regarding the same. How can we see the size of the bitmaps placed in the unmanaged heap using the Eclipse MAT tool. I am developing an application which is going out of memory even when the heap size in MAT shows only 6-8MB (Android 2.2). Since im using a lot of images I suspect that my unmanaged heap is growing to big, so it will really help if there was some way to see the images placed outside the managed heap.
Regards Kshitij
Patrick - May 17, 2011:
Hi Kshitij,
Take a look at slide 25, or the part that starts at around 15:00 in the video. That's the best way to see the size of your externally-allocated memory. You want to look at the first number.
Prior to Honeycomb, the backing memory for Bitmaps (allocated in unmanaged memory) is not visible in MAT.
Kshitij Aggarwal - May 17, 2011:
Thanks Patrick,
Ill debug it the old fashioned way then :)
Grace Yan - June 7, 2011:
Great talk. Thanks for all the useful info!
sandeep - July 18, 2011:
Thanks Patrick,
Excellent session. I am a beginner in android application development . I have a question on how to dereference the memory that has been allocated on the demand but not needed now for the application to run.
I have used a lot of heap memory for a file download from network . At the time of file download heap allocation gone upto 22MB. I have only left with 10MB for my bitmap decoding . But after the file download the used heap is deallocated but still i can get only 10MB for bitmap decoding even though 70% free of 22MB dalvik heap. So now my question is that is there any way that the free dalvik heap memory can be moved bitmap (native heap).