0

Possible Duplicate:
OutOfMemoryError: bitmap size exceeds VM budget :- Android

In my application I load some little Bitmap(between 2k and 300k) that I affect to ImageViews, during the first launch of my App it works fine but when I quit it and relaunch it, I always have the OutOfMemoryError during a Bitmap creation.

Could somebody tell me why?

Community
  • 1
  • 1
VinceFR
  • 2,551
  • 1
  • 21
  • 27

3 Answers3

0

You should use bitmap.recycle() manually when you leave your activity.

Please refer to http://developer.android.com/reference/android/graphics/Bitmap.html#recycle()

Jett Hsieh
  • 3,159
  • 27
  • 33
  • I was a litte bit feared by this kind of answer..So the GC is not very usefull for Bitmap..Thanks! – VinceFR Aug 02 '11 at 08:37
0

The problem is because your bitmap's size is too large than the VM can handle. For example from your code I can see that you are trying to paste an Image into imageView which you are capturing using Camera. So normally the camera images will be too large in size which will rise this error obviously. So as others have suggested, you have to compress your image either by sampling it or convert your image into smaller resolution. For example if your imageView is 100x100 in width and height, you can create a scaled bitmap so that your imageView gets filled exactly. You can do this for that,

Bitmap newImage = Bitmap.createScaledBitmap(bm, 350, 300,true);

And you have to recycle your bitmap and null your bitmap like this,

Bitmap bmap.recycle();
Bitmap bmap=null;
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
0

I really doubt you have to use bitmap.recycle() if you null your references, allowing GC (but please prove me wrong).

I'd say it's more likely your bitmaps are too large, possibly aggravated by long lived Context references or somesuch.

Pontus Gagge
  • 17,166
  • 1
  • 38
  • 51