Android – Bitmap cache takes a lot of memory

Images are also scaled according to the density so they can use a lot of memory.

For example, if the image file is in the drawable folder (which is mdpi density) and you run it on an xhdpi device, both the width and the height would double. Maybe this link could help you, or this one.

So in your example the bytes the image file would take are :

(1024*2)*(800*2)*4 = 13,107,200 bytes.

It would be even worse if you ran it on an xxhdpi device (like the HTC one and Galaxy S4) .

What can you do? Either put the image file in the correct density folder (drawable-xhdpi or drawable-xxhdpi) or put it in drawable-nodpi (or in the assets folder) and downscale the image according to your needs.

BTW you don’t have to set options.inJustDecodeBounds = false since it’s the default behavior. In fact you can set null for the bitmap options.

About down scaling you can use either google’s way or my way each has its own advantages and disadvantages.

About caching there are many ways to do it. The most common one is LRU cache. There is also an alternative I’ve created recently (link here or here) that allows you to cache a lot more images and avoid having OOM but it gives you a lot of responsibility.

Leave a Comment