java - Android - Bitmap cache takes a lot of memory -


i'm new memory management subject, there lot of things don't understand.
i'm trying cache image in app, i'm having troubles memory consumption:

all of bitmap chaching code pretty copy-pasted here: http://developer.android.com/training/displaying-bitmaps/index.html

i debugged code , checked heap size in ddms view in eclipse, , there 15mb jump after these code lines:

        options.injustdecodebounds = false;         return bitmapfactory.decoderesource(res, resid, options); 

in "decodesampledbitmapfromresource" method.

the image 1024x800, 75kb jpg file. according i've seen on internet, amount of memory image supposed take 1024*800*4(bytes per pixel)=3.125mb

all of threads regarding subject don't why it's taking more memory should. there way cache 1 image reasonable amount of memory?

edit

i tried using decodefile method suggested on @arshadparwez's answer below. using method, after bitmapfactory.decodestream method memory increased 3.5mb - problem solved, sort of, want cache bitmaps directly resource.

i noticed during decoderesource method there 2 memory "jumps" - 1 of 3.5mb - reasonable, , strange 1 of 14mb. 14mb used , why happen?

images scaled according density can use lot of memory.

for example, if image file in drawable folder (which mdpi density) , run on xhdpi device, both width , height double. maybe this link you, or this one.

so in example bytes image file take :

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

it worse if ran on xxhdpi device (like htc 1 , galaxy s4) .

what can do? either put image file in correct density folder (drawable-xhdpi or drawable-xxhdpi) or put in drawable-nodpi (or in assets folder) , downscale image according needs.

btw don't have set options.injustdecodebounds = false since it's default behavior. in fact can set null bitmap options.

about down scaling can use either google's way or my way each has own advantages , disadvantages.

about caching there many ways it. common 1 lru cache. there alternative i've created (link here or here) allows cache lot more images , avoid having oom gives lot of responsibility.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -