作者:XL | 来源:互联网 | 2023-05-17 10:31
Iamgettingtheerrorjava.lang.OutOfMemoryError:bitmapsizeexceedsVMbudget.我收到错误java.lang.Out
I am getting the error java.lang.OutOfMemoryError: bitmap size exceeds VM budget
.
我收到错误java.lang.OutOfMemoryError:位图大小超过VM预算。
This occurs when creating a bitmap for the purpose of manually drawing a line graph.
创建位图以便手动绘制折线图时会发生这种情况。
width = display.getWidth() - 10;
height = width * 4 / 5;
Bitmap emptyBmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Bitmap charty = createMyGraphAndStuff(emptyBmap);
It looks like the total memory allocated was about 700 Kb, an unreasonable amount.
看起来分配的总内存大约是700 Kb,这是一个不合理的数量。
I've seen other solutions invoked when creating bitmaps from a file, but here I am generating one myself. How can I minimize its memory footprint?
我已经看到了从文件创建位图时调用的其他解决方案,但在这里我自己生成一个。如何最小化其内存占用?
Here's some more code to give you a better idea of what it's doing:
这里有一些代码可以让您更好地了解它正在做什么:
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;
}
5 个解决方案
6
when the OOM occur, give some advice:
当OOM发生时,给出一些建议:
1.need know the oom occur position, the log information is enough
1.需要知道oom发生的位置,日志信息就足够了
2.the most time is the bitmap process, so you need know how much a image about used memory:
2.大部分时间是位图处理,所以你需要知道有关已用内存的图像:
the formula: w * h * every pixel token memory in byte
, if the Config
is Config.ARGB_8888
, every pixel token memory is 4bytes
, if it is the Config.RGB_565
, is 2bytes
.
公式:w * h *每个像素标记内存以字节为单位,如果Config为Config.ARGB_8888,则每个像素标记内存为4bytes,如果是Config.RGB_565,则为2bytes。
3.also, you need know the every app memory limits in your device:
3.此外,您需要知道设备中的每个应用程序内存限制:
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.getMemoryClass(); //return the memory size limit in MB
3.if the bitmap did not used again, try recyle()
it
3.如果位图没有再次使用,请尝试recyle()它
4.if began the bitmap process, the memory is almost the max memory limit, so use the adb shell dumpsys meminfo $pid
to check the memory usage, also the ddms - allocation tracker
is useful
4.如果开始进行位图处理,内存几乎是最大内存限制,所以使用adb shell dumpsys meminfo $ pid来检查内存使用情况,ddms - 分配跟踪器也很有用