Rob*_*Rob 1 memory graphics android graph bitmap
我收到了错误java.lang.OutOfMemoryError: bitmap size exceeds VM budget.
创建位图以便手动绘制折线图时会发生这种情况.
width = display.getWidth() - 10;
height = width * 4 / 5;
Bitmap emptyBmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Bitmap charty = createMyGraphAndStuff(emptyBmap);
Run Code Online (Sandbox Code Playgroud)
看起来分配的总内存大约是700 Kb,这是一个不合理的数量.
我已经看到了从文件创建位图时调用的其他解决方案,但在这里我自己生成一个.如何最小化其内存占用?
这里有一些代码可以让您更好地了解它正在做什么:
public Bitmap DrawTheGraphAndStuff(Bitmap bitmap, String[] scores)
{
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
drawTheGridLines(canvas);
plotTheDataPoints(canvas , scores , "the title" , 0 );
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Run Code Online (Sandbox Code Playgroud)
当OOM发生时,给出一些建议:
1.需要知道oom发生的位置,日志信息就足够了
2.大部分时间是位图处理,所以你需要知道有关已用内存的图像:
公式:w * h * every pixel token memory in byte如果Config是Config.ARGB_8888,则每个像素令牌记忆是4bytes,如果是Config.RGB_565,则是2bytes.
3.此外,您需要知道设备中的每个应用程序内存限制:
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.getMemoryClass(); //return the memory size limit in MB
Run Code Online (Sandbox Code Playgroud)
3.如果位图没有再次使用,请尝试recyle()一下
4.如果开始了位图进程,内存几乎是最大内存限制,所以adb shell dumpsys meminfo $pid用来检查内存使用情况,也ddms - allocation tracker很有用