I am getting the 'java.lang.OutOfMemoryError: bitmap size exceeds VM budget' error... What I am doing is that I download ~50 images from server and store them in the memory.
When I first saw this problem I optimize our images to download thumbnails only (50 JPEGS, ~12kB each, size 160x240px) and I start releasing them by calling Bitmap.recycle() as soon as I do not need them. Everything is working on newer phones, where the process can get 32MB, but on older phones with 16MB max not.
So my questions are:
Is it correct that all processes are limited to 32MB or less on older phones (i.e. the memory the Runtime.getRuntime().maxMemory() call returns) and this memory is for the Dalvik, framework, application and data?
Can we use more memory in application then amount returned by Runtime.getRuntime().maxMemory() ?
How I should optimize the images - they are anyway pretty small now... Should I start swapping them to internal flash/SD? I am keeping them in memory as long as they are shown in the GridView...
I appreciate any help - I went through the similar thread here and done what I have seen there... but still it is not enough...
Edit update
I added those comments after weakwire
answer.
• In my case I download ~50 images and store all of them in Bitmap objects in memory. See below why I am doing that.
• How much memory occupies the image in the Bitmap object? Is it there in raw format means that if image is 160x240x24bpp
it occupies ~115 kB
in memory, so 50 images are ~6MB
? Is that correct?
• I have also read that images are not part of my process heap, but they are counted, so using the maxMemory(), totalMemory(), freeMemory() cannot really help. Is that correct?
• My application UI has complete custom graphics (60 png files, many of them in draw9pacth format)
• I have no problem on 24/32 MB devices, I have problem only on 16 MB devices
• As soon as I download all 50 images and store them into memory I show them in the 2 columns width GridView. I think for this I need to keep all images on the heap (saving to internal flash I think is not solution there). Is that correct assumption?
• When I initialize the application and the UI is loaded I can see that I have 448kB free memory of 5459kB allocated of the whole 16384kB budget. I assume I cannot really use the whole 16384kB + if I understand it correctly I cannot really see from maxMemory(), totalMemory(), freeMemory() how much images that are already loaded occupy, since the dalvik-vm does not 'see' this heap. Assuming that ~2M are already used and I can use up to ~14MB I have 5.5MB left, so I can easily with 50 images in the memory run into OOM problems… Is that correct computation?
I WILL definitely go over the suggested video – it looks nice – but weakwire
, please try to answer me my questions… Thanks for that and for your time!