android - out of memory exception when creating bitmap

I am getting the following error after creating bitmap second time around:

04-17 18:28:09.310: ERROR/AndroidRuntime(3458): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

this._profileBitmap = Bitmap.createBitmap(_profileBitmap, xCoor,  yCoor, width, height);

From log:

04-17 18:27:57.500: INFO/CameraCropView(3458): Original Photo Size: W 1536 x H 2048   
04-17 18:28:06.170: INFO/CameraCropView(3458): xCoor: 291   
04-17 18:28:06.170: INFO/CameraCropView(3458): yCoor: 430    
04-17 18:28:06.170: INFO/CameraCropView(3458): Width: 952  
04-17 18:28:06.170: INFO/CameraCropView(3458): Height: 952  

Since the image is huge I get the error. But the interesting thing is the error does not happen the first time, only when I take the picture the second time, which makes me believe this profileBitmap is NOT destroyed. How do I clean this up?


I had the same problem and fix it this way:

My app was ~18MB size, and when I see how much memory left free I was shocked - 654KB (on 1GB RAM!). So I just deleted almost all images from project and downloaded them from the internet on first start, and use pics from SD card when needed.

To check total/free memory for your app use:

 Runtime.getRuntime().totalMemory();
 Runtime.getRuntime().freeMemory();

EDIT: I forgot the main thing - add in your manifest, between application tag, this line:

android:largeHeap="true"


There are many problems with memory exceptions with bitmaps on Android, many of which are discussed on stackoverflow. It would probably be best if you went through the existing questions to see if yours matches one of the existing ones, then if not, write up what makes your situation different.

Some examples:

Out of memory exception due to large bitmap size

Android: out of memory exception in Gallery

Android handling out of memory exception on image processing

etc: https://stackoverflow.com/search?q=android+out+of+memory+exception+bitmap


android bitmap processing tips

Now here are tips which you can follow and can avoid out of memory exception in your Android Application.

  1. Always use Activity context instead of Application context. because Application context cannot be garbage collected. And release resources as your activity finishes. (life cycle of object should be same as of activity).

2 . When Activity finishes. Check HEAP DUMP (memory analysis tool in Android studio).

If there are objects in HEAP DUMP from finished activity there is memory leak. review your code and identify what is causing memory leak.

  1. Always use inSampleSize

Now what is inSampleSize ? with the help of inSampleSize you are actually telling the decoder not to grab every pixel in memory, instead sub sample image. This will cause less number of pixels to be loaded in memory than the original image. you can tell decoder to grab every 4th pixel or every second pixel from original image. if inSampleSize is 4. decoder will return an Image that is 1/16 the number of pixels in original image.

so how much memory you have saved ? calculate :)

  1. Read Bitmap Dimensions before loading into memory.

    How reading bitmap dimensions before loading image into memory can help you avoid out of
    memory error ? Let's Learn

    use inJustBounds = true

here is technique with the help of which you can get image dimension beore loading it in memory

 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = true;
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.id.myimage,    options);
 int imageHeight = options.outHeight;
 int imageWidth = options.outWidth;
 String imageType = options.outMimeType;

Above code snippet will not give us any image/bitmap. it will return null for bitmap Object. but it will definitely return width and height of that image. which is R.id.myimage.

Now you have width and height of Image. you can scale up or scale down image based on these factors:

  • ImageView size which will be used to display Image.
  • Available amount of memory. you can check available amount of memory using ActivityManager and getMemoryClass.
  • Screen size and density of device.

    1. Use appropriate Bitmap Configuration

    Bitmap configurations is color space/color depth of an Image. Default bitmap Configuration in Android is RGB_8888 which is 4 bytes per pixel.

If you use RGB_565 color channel which use 2 Bytes per pixel. half the memory allocation for same resolution :)

  1. Use inBitmap property for recycling purpose.

  2. Do not make static Drawable Object as it cannot be garbage collected.

  3. Request large heap in in manifest file.

  4. Use multiple processes if you are doing lot of image processing(memory intensive task) or use NDK (Native Development using c, c++)


This happens because you are loading the bitmap directly,which consumes a lot of memory. Instead use a scaled down version of the picture in _profileBitmap. This guy explains it pretty well. http://androidcocktail.blogspot.in/2012/05/solving-bitmap-size-exceeds-vm-budget.html