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.

3
  • 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? Commented Jan 23, 2013 at 7:57
  • I get the bmp by calling BitmapFactory.decodeByteArray(data, 0, data.length); Commented Jan 23, 2013 at 8: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? Commented Jan 23, 2013 at 8:04

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@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!
So I want to take a picture and pass it to the native layer, using JNI to do some computation, what can I do?
Make sure that your image fits in your memory (using the hints I provided), there is nothing else you can do.
0

Call the Garbage Collector manually.

System.gc();

Comments

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();
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.