wolfkin
wolfkin

Reputation: 31

please help me get over java.lang.OutOfMemoryError: bitmap size exceeds VM budget

i have to display 20 same balls jump at the same style at different positions. but i keep getting the error: java.lang.OutOfMemoryError: bitmap size exceeds VM budget .

at first i tried with xml files in anim folder. but i can't release any resource declared in xml. so i changed to create Bitmap with BitmapFactory, and call recycle function. but i can't get over either.

the code flow is something like this:

Bitmap[] ballBitmap;
int[] playOrder = new int[2];   //the balls will play in order of the definiens in playOrder
ImageView[] balls = new ImageView[20];  //it contains 20 balls

private void iniNewQuestion() {
passNum++;
if(passNum > 1) clearPic();
if(passNum < 20) playBall();
}


private void playBall() {
int ballIndex = playOrder[passNum - 1] - 1;
ballBitmap = new Bitmap[9];
ballBitmap[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_01);
ballBitmap[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_02);
ballBitmap[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_03);
ballBitmap[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_04);
ballBitmap[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_05);
ballBitmap[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_06);
ballBitmap[6] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_07);
ballBitmap[7] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_08);
ballBitmap[8] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_09);

AnimationDrawable ballAnimation = new AnimationDrawable();
for (int picIndex = 0; picIndex < 9; picIndex++) 
{
    ballAnimation.addFrame(new BitmapDrawable(ballBitmap[picIndex] ), breakTime[picIndex]);
    //if(picIndex > 0) ballBitmap[picIndex].recycle();
}

ballAnimation.setOneShot(true);
balls[ballIndex].setBackgroundDrawable(ballAnimation); 
ballAnimation.start();

Handler toNextQuestionHandler = new Handler();
toNextQuestionHandler.postDelayed(runIniNewQuestion, 3000);
}

private Runnable runIniNewQuestion = new Runnable() { public void run() { iniNewQuestion(); } };

private void clearPic() {
    int picIndex = 0; 
    for (picIndex = 0; picIndex < 9; picIndex++) 
    {
            if(ballBitmap[picIndex] != null ) 
            {
                    if(!ballBitmap[picIndex].isRecycled()) ballBitmap[picIndex].recycle();
            }
    }
}

it always happens when I play the 6th ball. the error log is:

01-28 05:29:31.927: E/AndroidRuntime(1090): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.Bitmap.nativeCreate(Native Method) 01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.Bitmap.createBitmap(Bitmap.java:468) 01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.Bitmap.createBitmap(Bitmap.java:435) 01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340) 01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488) 01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462) 01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323) 01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:346) 01-28 05:29:31.927: E/AndroidRuntime(1090): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:372) 01-28 05:29:31.927: E/AndroidRuntime(1090): at com.gzconcern.animalaba.AbaGame.playBall(AbaGame.java:533)

I thought when I add a frame to ballAnimation, i have new a Bitmap from the picture, so I should release the picture right away. but when I do call recycle, it turns out this:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@44e85c60

Upvotes: 0

Views: 670

Answers (1)

Saurabh Verma
Saurabh Verma

Reputation: 6728

It's a common issue when using heavy bitmaps at runtime. Hopefully some of the solutions given here will work for you http://code.google.com/p/android/issues/detail?id=8488 Another thing which you should do is monitor the runtime memory of your application in DDMS. To monitor it, select the device, then your application. Then select the green icon at the top which says 'Update Heap'. Then open the window for 'Heap' and select 'Cause GC'. There you will see the memory available for you application and the actual memory consumed by your app.

Upvotes: 1

Related Questions