1

The problem occurs at the last line, when I try to new such an array, it collapses. In Logcat, it says "java.lang.OutOfMemoryError". What's that mean? Please help.

Bitmap bmap;
Bitmap bMapRotate;
int bmapWidth;
int bmapHeight;
int[] bmapColorArray;
int bmapColorArraySize;

bmapWidth = bmap.getWidth();
bmapHeight = bmap.getHeight();
bmapColorArraySize = bmapWidth * bmapHeight;
bmapColorArray = new int[bmapColorArraySize];
bmap.getPixels(bmapColorArray, 0, bmapWidth, 0, 0, bmapWidth, bmapHeight);

And then call passing this int Array to the native layer.

Gabriel
  • 237
  • 3
  • 9
  • 1
    It means that you tried to allocate more memory than is available. An int array needs four bytes per entry. How big is that bitmap? – Thilo Jan 23 '13 at 07:57
  • I get the bmp by calling BitmapFactory.decodeByteArray(data, 0, data.length); – Gabriel Jan 23 '13 at 08:00
  • OK. I tried a small number, so it's ok. That is, the bmpColorArraySize is so large that the heap memory can't afford it. So this way may not work. The thing is that I want to take a picture and pass it to the native layer, using JNI to do some computation, what should I do? – Gabriel Jan 23 '13 at 08:04

3 Answers3

3

Android applications have a limited amount of memory available. Depending on the screen resolution/size from 16MB till 24MB (IIRC).

So, if you allocate several arrays for large images (10M pixel) you mememory is full in no time.

Try to store only the necessary information. If you are using a scaled version of an image (which you probably are) store the scaled version instead of the full-blown original. Also, do not keep images in memory that you are (likely) not going to use again.

In other words, try to be conservative with your memory, especially with large memory allocations.

Veger
  • 37,240
  • 11
  • 105
  • 116
  • @nlsbshtr Yes. It helps with throwing away unused bitmaps. It does not help with only storing scaled ones as well. That is still something that needs to be done! – Veger Jan 23 '13 at 08:04
  • So I want to take a picture and pass it to the native layer, using JNI to do some computation, what can I do? – Gabriel Jan 23 '13 at 08:07
  • Make sure that your image fits in your memory (using the hints I provided), there is nothing else you can do. – Veger Jan 23 '13 at 08:41
0

Call the Garbage Collector manually.

System.gc();
Naskov
  • 4,121
  • 5
  • 38
  • 62
0

Try this if you want to avoid from crashing.

 Runtime runtime = Runtime.getRuntime();  

    Log.d("Free memory",""+runtime.freeMemory());  

    if(runtime.freeMemory()>(h*w)) 
    {
        int[] i = new int[h* w];
    }
    else
    {
        Toast.makeText(getApplicationContext(),"No More Heap memory",Toast.LENGTH_LONG).show();
     }
Govinda P
  • 3,261
  • 5
  • 22
  • 43