0

hello i know this is a dupicat tread but the answer did not seem to work for me my app crashes on second time running so if i run it once its ok but when i close and run again it crashes giving me this error in the log cat java.lang.OutOfMemoryError: bitmap size exceeds VM budget i tried using this code

@Override
protected void onDestroy(){
    // TODO Auto-generated method stub
    super.onDestroy();
    unbindDrawables(sView);
    System.gc();
}

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

but no luck i am drawing my bitmaps on a canvas using the surfaceview class this is my code private SurfaceV sView;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    sView = new SurfaceV(this);
    setContentView(sView);
}

and in the sView class

private SurfaceHolder holder;
private Thread thread = null;
private Canvas canvas;
private Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.sky);
private Bitmap bg = Bitmap.createScaledBitmap(background, w, h, false);
private Bitmap sky = BitmapFactory.decodeResource(getResources(), R.drawable.sky2);
private Bitmap s = Bitmap.createBitmap(sky, 210, 10,  230, 230);
 private Bitmap c1 = Bitmap.createBitmap(sky, 10, 14,  182, 100);
private Bitmap c2 = Bitmap.createBitmap(sky, 10, 142,  182, 100);
private Bitmap c3 = Bitmap.createBitmap(sky, 10, 292,  182, 112);
private Bitmap sun = Bitmap.createScaledBitmap(s, sunSize, sunSize, false);
private Bitmap cloud1 = Bitmap.createScaledBitmap(c1, cloudWidth, cloudHeigth, false);
private Bitmap cloud2 = Bitmap.createScaledBitmap(c2, cloudWidth, cloudHeigth, false);
private Bitmap cloud3 = Bitmap.createScaledBitmap(c3, cloudWidth, cloudHeigth, false);

public SurfaceV(Context context){
    super(context);
    holder = getHolder();
}

@Override
public void run(){
    while(running){
        if(!holder.getSurface().isValid()){
            continue;
        }

        startTime = System.currentTimeMillis();

        canvas = holder.lockCanvas();
        draw(canvas);
        holder.unlockCanvasAndPost(canvas);

        doFpsCheck(startTime);
}

@Override
public void draw(Canvas canvas){
    super.draw(canvas);
    canvas.drawBitmap(bg, 0, 0, null);
    canvas.drawBitmap(sun, 20, 20, null);

    canvas.drawBitmap(cloud1, c1x, c1y, null);
    canvas.drawBitmap(cloud2, c2x, c2y, null);
    canvas.drawBitmap(cloud3, c3x, c3y, null);
}

please any help will be very much appriciated thank you in advance

1 Answer 1

0

The memory leak in Android when you use Bitmaps is very popular. It's because you create many bitmaps, display them, but not removing them from memory, so suddenly you will get out of memory error.

After creating bitmaps and drawing them you should use Recycle method:

yourBitmap.recycle()

This is form official documentation:

"Caution: You should use recycle() only when you are sure that the bitmap is no longer being used. If you call recycle() and later attempt to draw the bitmap, you will get the error: "Canvas: trying to use a recycled bitmap"

More you can find here:

https://developer.android.com/training/displaying-bitmaps/manage-memory.html

Hope it will help.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.