android bitmap cache takes a lot of memory ¬†using -'java,android,caching,bitmap'
I'm new to all the memory management subject, so there are a lot of things I don't understand.
I'm trying to cache an image in my app, but I'm having troubles with its memory consumption:
All of the Bitmap Chaching code is pretty much copy-pasted from here: http://developer.android.com/training/displaying-bitmaps/index.html
I debugged the code and checked the heap size in the DDMS view in eclipse, and there is about 15mb jump after these code lines:
¬†¬†¬†¬†¬†¬†¬†¬†options.inJustDecodeBounds = false;
¬†¬†¬†¬†¬†¬†¬†¬†return BitmapFactory.decodeResource(res, resId, options);
in the "decodeSampledBitmapFromResource" method.
The image is 1024x800, 75kb jpg file. According to what I've already seen on the internet, the amount of memory this image is supposed to take is about 1024*800*4(Bytes per pixel)=3.125mb
All of the threads regarding this subject don't say why it's taking much more memory than it should. Is there a way to cache one image with a reasonable amount of memory?
I tried using the decodeFile method suggested on @ArshadParwez's answer below. Using this method, after the BitmapFactory.decodeStream method the memory is increased by only 3.5mb - problem solved, sort of, but I want to cache bitmaps directly from the resource.
I noticed that during the decodeResource method there are 2 memory "jumps" - one of about 3.5mb - which is reasonable, and another strange one of 14mb. What are those 14mb used for and why does this happen?