开发者

Memory usage between Android 2.2 and 2.3

I am developing an app that contains many images. I have noticed that the memory usage of my app is quite a bit higher in Android 2.3 than in Android 2.2 - ultimately resulting in OutOfMemory errors while loading bitmaps. In researching this, I logged the result of Debug.getNativeHeapAllocatedSize() in the onResume of the first activity. In Android 2.2, this outputs 5MB. In Android 2.3, this outputs 17MB.

I created a sample application to try to further narrow this down. The sample app has 2 activities: the first has a small image and a button to launch the second activity; the second activity has several large images in a ScrollView. You can download the sample here: http://dl.dropbox.com/u/21709517/MemoryUsageExample.zip When running this sample, Debug.getNativeHeapAllocatedSize() reports 3MB allocated after starting the first activity in both 2.2 and 2.3. After launching the second activity, Android 2.2 still reports 3MB allocated while Android 2.3 reports 17MB allocated. Running "adb shell dumpsys meminfo test.example" shows the same types of results.

Why is there such a dramatic difference between Android 2.2 and 2.3? And more importantly, what should I be doing differently to prevent using so much memory on Android 2.3?

-----UPDATE-----

After looking at the information Romain Guy posted, I decided to do a bit more playing with the various loading options. The result shows that forcing the images to be loaded as RGB_565 does reduce the memory in Android 2.3, however it still uses more than 2.2. In my example app, Android 2.3 now uses 10MB vs Android 2.2 using 3MB. Further, I tried to force 32-bit by using ARGB_8888. In this case, Android 2.3 uses 17MB, but Android 2.2 continues to only use 3MB. The updated sample can be found here: http://dl.dropbox.com/u/21709517/MemoryUsageExample2.zip

What is the cause for these differences? Is there more that needs to be done to get the memory usage closer to Android 2.2? Also, is there a way to force images created via xml layouts into a particular format? Can this be开发者_C百科 set application-wide? Thanks.

-----UPDATE 2-----

According to this thread, it is not possible to force images loaded via xml inflation to be a particular format.

I am still hoping for an answer on why Android 2.2 and Android 2.3 use different amounts of memory after forcing 16 or 32 bit.


Android 2.3 loads images in 32 bits by default now. You can force 16 bits format using BitmapFactory.Options.

See http://www.curious-creature.org/2010/12/04/gingerbread-and-32-bits-windows/ and http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/


My guess is that. Android OS preloads some images into native heap. The number of images preloaded depends on the OS VM budget for each process. Since 2.3 increased the VM budget to 32M, so it preloaded more images for each process than on 2.2 devices. Hence you saw more native heap usage in 2.3. This is only my guess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜