How to avoid OutOfMemory Error / OOM (The Ugly Truth Revealed)
ERROR/dalvikvm-heap(4204): 691456-byte external allocation too large for this process.
01-25 22:13:18.694: ERROR/(4204): VM won't let us allocate 691456 bytes 01-25 22:13:18.694
If you have ever got the message above, you are at the right place.
First of all the reason:
Heap size != External Memory size
Dalvik's external memory is limited to ~ 4MBs for each process (That is the Ugly Truth). If it overflows, you get the BitmapFactory.DecodeFile Error.
That's why even if you have like 2MB-s heap memory free, VM won't let you allocate ~700KB.
Since you can't modify external memory's size, you have to reduce your memory usage. This is the only solution I have, but it really works at least.
The solution:
Let say you have a nice big Gallery with lots of large images. An ImageAdapter class will provide all of the images that you are going to use. Therefore you'll need a List of ImageViews.
Now. Once you have set an ImageView, even if you're not using it, even if you call recycle() on Bitmap in it, it stays in that 4MB external memory. The only solution is not to use more ImageViews than 5 or maximum 10 at a time.
How can you do it? Well that's the tricky part. Probably you have to write an algorythm in your ImageAdapter Class for it, that can manage to free the ImageView-s you are not using, with calling the
- myImageView.setImageBitmap(null);
or if you are adventurous enough, :) can use a small "loading" image from Resource, while you don't have to show the original picture. But be careful, because this can also eat your 4MB memory if you use it wrong.
- myImageView.setImageResource(R.drawable.loadingImage);
Hope this helps, let me know what happens!
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Maudyp17 (Malcolm Yong)I have just discovered territory 3 in My Railway! You can get it for FREE on Android! http://t.co/lWE84eeL #android #androidgames
14 min 45 sec ago -
@USandroidtablet (USA Android Tablet)USA Android: Android OS 2.3 Touch Screen 7" inch Mid Tablet PC HDMI WIFI MID 1GHz 512MB 4GB #android http://t.co/9sDVUCbP #android #tabletpc
14 min 46 sec ago -
@NeeNeeDahl (Carmen M. Dahl)I've just received an achievement: Cartographer https://t.co/CaoA5G0q #Android #Androidgames
14 min 48 sec ago -
@AshleyLynnLytle (Ashley Lytle)
just reached level 9 on Rock The Vegas on my Android http://t.co/r86NCvaT #Android #Androidgames
14 min 48 sec ago -
@belindassy (Belinda Soon)I have just connected Farm in My RailWay. You can get it for FREE on Android! http://t.co/Y7Sw9NjS #android #androidgames
14 min 49 sec ago
Poll
Useful resources
Android Development Projects
- Android App wanted immediately by JoePublic
- LIST DATA PROJ by nhammoud
- Nonpublic project #1433932 by subpariq
- Alarm Android Application Design by globalheed
- Simple Album App for Android by ayfonfan
- iOS and Android photo manipulation 'Morph App' by whatwedomedia
- Onsite Software Engineers in Germany by sudhirshree
- Augmented reality by merder99
- Nonpublic project #1433560 by vobla73
- Mobile app coder needed for quick, simple app by Ergometrix



