java.lang.OutOfMemoryError: bitmap size exceeds VM budget crashes on second time running app

java.lang.OutOfMemoryError: bitmap size exceeds VM budget crashes on second time running app

你好,我知道这是一个 dupicat 踏板,但答案似乎对我不起作用 我的应用程序在第二次 运行ning 时崩溃,所以如果我 运行 它一次没问题,但是当我关闭并再次 运行 它崩溃,在日志猫中给我这个错误 java.lang.OutOfMemoryError: 位图大小超出 VM 预算 我尝试使用此代码

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

但运气不好,我正在使用表面视图 class 在 canvas 上绘制我的位图,这是我的代码 私人 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);
}

并在 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);
}

非常感谢任何帮助提前谢谢你

使用位图时 Android 中的内存泄漏非常普遍。 这是因为你创建了很多位图,显示它们,但没有从内存中删除它们,所以突然你会出现内存不足的错误。

创建位图并绘制它们后,您应该使用回收方法:

yourBitmap.recycle()

这是表格官方文档:

"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: 尝试使用回收位图

您可以在这里找到更多内容:

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

希望对您有所帮助。