3

i have a custom list view. in that i am listing out images of around 200. The images are been stored in my drawable. I have converted the image to be a bitmap and i am displaying the image as bitmap. Every time when the activity is being opened the images get listed out. Like this when i am calling the activity for around 10 times it gets crashed and the error is

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

then i recycled the bitmaps and by using System.gs function i got the following

Clamp target GC heap from 25.464MB to 24.000MB
GC freed 322 objects / 70664 bytes in 146ms
 Clamp target GC heap from 25.471MB to 24.000MB
 GC freed 2 objects / 48 bytes in 138ms
Forcing collection of SoftReferences for 10720-byte allocation
 Clamp target GC heap from 25.269MB to 24.000MB
 DEBUG/dalvikvm(15311): GC freed 5080 objects / 211168 bytes in 121ms
 ERROR/dalvikvm-heap(15311): Out of memory on a 10720-byte allocation

Now atlast i got this Out of memory on a 10720-byte allocation error

Now how to resolve the error, please help me friends

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
Siva K
  • 4,968
  • 14
  • 82
  • 161

2 Answers2

2

Let's run through the checklist for your situation:

  1. Is a new activity created every time and you forget to finish out the previous one? If you did, make sure that you profile the app by dumping the memory in .hprof file and use MemoryAnalyzer (http://www.eclipse.org/mat/). Check the number of instances of activity that you have in memory to make sure that it is actually get cleaned up.
  2. If you are reloading the same data, you might want to keep this activity around and use FLAG_ACTIVITY_REORDER_TO_FRONT when starting it so it doesn't need to recreate
  3. How big is your drawable? You might want to resize it before you set it to your ImageView

Let me think up some more that might be helpful and add later

momo
  • 21,233
  • 8
  • 39
  • 38
1

Have you tried setting the Bitmap variables as Soft-references?

HAxxor
  • 1,164
  • 3
  • 16
  • 28
  • +1 but I think you need to explain a little more on what is soft-reference. – ming_codes Aug 12 '11 at 17:28
  • Basically you have a few types of references that you can declare a variable. One of them being Softreferences, another being WeakReferences. There are a couple more.. But I forget them off the top of my head. In general, what it does is declare when a variable will be collected by the garbage collector by giving it a certain hierarchy of the reference. Here is a good link giving a very clear description. A lot better than mine... [References](http://weblogs.java.net/blog/2006/05/04/understanding-weak-references) – HAxxor Aug 12 '11 at 18:39
  • The use of Java soft references is apparently not recommended for Android apps. See here http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html and here http://stackoverflow.com/questions/11972441/android-outofmemory-within-different-devices – RenniePet Oct 15 '15 at 21:31