0

I have a routine that manipulates a Bitmap to convert form RGB to Grayscale.

It works fine usually, but when I try to use it on a Bitmap which is 1088kb in size it gives me this Error:

java.lang.OutOfMemoryError

I am using the Emulator. 1088kb is not a very big picture, how can it be exhausting the memory?

To be precise the application that does invoke the problematic code includes another Activity on the back-stack that has a ListView of pictures thumbnails.

This is the method:

public Bitmap toGrayscale(Bitmap bmpOriginal)
{        
    int width, height;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();    

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}
il_guru
  • 8,383
  • 2
  • 42
  • 51
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157

1 Answers1

3

You are being very inefficient in managing bitmaps in memory and you likely have a memory leak (not freeing bitmaps from memory when you are done with them or keeping them around in activities that don't get garbage collected). The Android Developers have a page for proper bitmap management:

http://developer.android.com/training/displaying-bitmaps/index.html

You can try increasing the Heap size on your emulator.

Out of memory error on android emulator, but not on device

Monitor the heap size on your emulator:

https://stackoverflow.com/a/7427803/445131

Get rid of your memory leaks:

What Android tools and methods work best to find memory/resource leaks?

Community
  • 1
  • 1
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335