1

Android bitmap size exceeds VM budget.

My app is getting this error frequently. I have two questions.

  1. Do I need to recycle my about activity (it contains some imageviews and buttons and textViews)?
  2. What is the difference between .recycle(); and between system.gc(); ?
Jim Garrison
  • 83,534
  • 20
  • 149
  • 186
John Jared
  • 790
  • 3
  • 16
  • 40
  • Refer this answer http://stackoverflow.com/questions/11373240/android-error-java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget/11373278#11373278 and This to http://stackoverflow.com/questions/3823799/android-bitmap-recycle-how-does-it-work – Aerrow Aug 13 '12 at 16:58

3 Answers3

2

Try watching this video (romain guy):

http://www.youtube.com/watch?v=duefsFTJXzc&list=PLD1B287286E23E2D1&index=1&feature=plpp_video

It will provide some insight into best practices for Bitmaps.

JoxTraex
  • 13,253
  • 6
  • 31
  • 45
  • how can you ask for help then complain the video is too long??? You have to as least put some effort in! – Bear Aug 13 '12 at 17:20
  • Not everyone has unlimited access. I live in the sticks and access via satellite, where I have a daily cap of about 400 MB, so don't be so quick to criticize if someone says something is too large, long, whatever. It could literally be true. – Barak Aug 13 '12 at 17:57
2

You should always try and recycle Bitmaps afte you have used them.

As far as I understand, you should try and avoid calling system.gc(). Calling recycle() will allow the bitmap object to be garbage collected.

I hope this helps.

Luke Taylor
  • 9,223
  • 13
  • 40
  • 72
  • I made a new post with better infos http://stackoverflow.com/questions/11942707/android-bitmap-size-exceeds-vm-budget-when-dealing-with-bitmaps – John Jared Aug 13 '12 at 21:42
0

I got the same problem while picking images from camera.
I resized the bitmap of image using following code:

Bitmap bitmap = resizeBitMapImage(picturePath, 75, 91);
            profilePic.setImageBitmap(bitmap);

private Bitmap resizeBitMapImage(String filePath, int targetWidth,
        int targetHeight) {

    Bitmap bitMapImage = null;
    // First, get the dimensions of the image
    Options options = new Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    double sampleSize = 0;
    // Only scale if we need to
    // (16384 buffer for img processing)
    Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
            .abs(options.outWidth - targetWidth);

    if (options.outHeight * options.outWidth * 2 >= 1638) {
        // Load, scaling to smallest power of 2 that'll get it <= desired
        // dimensions
        sampleSize = scaleByHeight ? options.outHeight / targetHeight
                : options.outWidth / targetWidth;
        sampleSize = (int) Math.pow(2d,
                Math.floor(Math.log(sampleSize) / Math.log(2d)));
    }

    // Do the actual decoding
    options.inJustDecodeBounds = false;
    options.inTempStorage = new byte[128];
    while (true) {
        try {
            options.inSampleSize = (int) sampleSize;
            bitMapImage = BitmapFactory.decodeFile(filePath, options);

            break;
        } catch (Exception ex) {
            try {
                sampleSize = sampleSize * 2;
            } catch (Exception ex1) {

            }
        }
    }
     return bitMapImage;
}
Navnath Godse
  • 2,235
  • 2
  • 22
  • 32
Prachi
  • 3,476
  • 2
  • 22
  • 39