Sunday, December 29, 2013

OutOfMemoryError in Android development

If you are using Images in your Android app, sometimes you may face this run-time issue in Android development. This happens only in some of the Android devices. This error does not show up in the Android emulator. So until you test on some devices you can't find this error. The reason for this error is that there are security limitations in the Android OS. If restricts the memory of RAM to 16 MB for a single app. So even if you have 2 GB ram on your Android device, a single app can consume only up-to 16MB. That is the normal procedure.

Once I had to used twelve 1800*1700 pixel images some other images. So lets do a simple calculation.

1800*1700*3    = 9180000 bits
9180000 / 8       = 1147500 bytes
1147500 / 1024 = 1120.60 kB   nearly equals to 1MB.

So each image costs nearly 1 MB in the RAM.
I had 12 of these images. Total of 12 MB.

Also I had some more images and my total memory of the app exceeds 16MB limit. So in some devices I got OutOfMemoryError in Bitmap factory.

The simple solution I followed is, I reduced the resolution of the images. Because 1800*1700 is way too much for an Android app. After reducing the resolution the error is fixed.

Please refer this developer document for more details on this issue.

No comments:

Post a Comment