0

I have a repeated Runnable task that performs a setImageResource() every 10 seconds (the resource is a jpeg file). This is the only significant thing happening in the activity.

However, after a few iterations (2-4), the app crashes with the following error:

  • VM won't let us allocate X bytes
  • Shutting down VM

I did not expect this. Why isn't the GC cleaning up previous jpeg bitmaps? How do I avoid crashing the VM?

Thanks.

Richard Eng
  • 1,744
  • 6
  • 20
  • 30

2 Answers2

1

You have to call recycle on previous bitmaps. Android is allocating memory for image recources and is not freeing it until you call recycle method with your hands.

Orest
  • 1,667
  • 4
  • 17
  • 27
  • recycle() isn't working for me. There appears to be a hard 7MB limitation in the VM manager. (My jpegs are 1024x768, which can expand into fairly large bitmaps.) Ironically, the iPhone has no problem with these same bitmaps, so I am profoundly disappointed with Android's limitation. – Richard Eng Jan 15 '12 at 19:21
  • I should mention that on the iPhone, I had to avoid using the imageNamed: method for loading these jpegs because the method caches them in the System Cache. (I used a different method, instead.) In no time, the System Cache overflows and causes the app to crash. Is it possible that Android has a similar issue? – Richard Eng Jan 15 '12 at 19:30
  • http://developer.android.com/guide/topics/data/data-storage.html this is a post where you can find some info about caching files in android. – Orest Jan 15 '12 at 19:34
  • recycle() isn't working, I suppose the only way is to compress your bitmaps. – Orest Jan 15 '12 at 19:35
  • I've done that and it seems to solve the problem. My bitmaps are now slightly smaller than the Nexus S screen. However, just out of curiosity, does this mean that 10" tablets will never enjoy high-res images? (I'm assuming Android tablets don't have ample memory.) – Richard Eng Jan 15 '12 at 20:21
  • You may try to store the original images and compressing them in runtime in some background thread. How do like this idea? – Orest Jan 15 '12 at 20:28
0

Have you tried setting the image to null before setImageResource():

object.image = null;
object.setImageResource();
Yaqub Ahmad
  • 26,916
  • 22
  • 99
  • 146
  • Yes, I tried this, too. Same 7MB limitation in the VM manager. I'm astonished that no one else had run into this problem. – Richard Eng Jan 15 '12 at 19:23