Monkey test fails due to createBitmap reports OOM

In my custom View class I create a bitmap when view size is determined.

public void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w == 0) return;
    mBoard = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
    // draw something on mBoard, to reduce load of onDraw
}

In onDraw, draw mBoard to canvas and something others.

Unfortunately createBitmap reports OOM sometimes during monkey test, not the first time the view loads. Monkey test will enter/quit activity many times in short time.

I guess mBoard is created but not freed after activity quits, so next time entering activity another memory block is allocated. To fix this, in activity's onDestroy(), call View's recycle() method.

void recycle() {                                                                                                              
    if (mBoard != null) {
        mBoard.recycle();
        mBoard = null;
    }
}

With recycle called, I still found no difference when checking free/total heap memory with Runtime.getRuntime().getFree/MaxMemory(). I think monkey test may still fail. Can anyone help?

链接地址: http://www.djcxy.com/p/93070.html

上一篇: Android:将大型位图文件调整为缩放输出文件

下一篇: 由于createBitmap报告OOM,Monkey测试失败