I am getting the error java.lang.OutOfMemoryError: bitmap size exceeds VM budget.
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.
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;
}
when the OOM occur, give some advice:
1.need know the oom occur position, the log information is enough
2.the most time is the bitmap process, so you need know how much a image about used memory:
the formula:
w * h * every pixel token memory in byte, if theConfigisConfig.ARGB_8888, every pixel token memory is4bytes, if it is theConfig.RGB_565, is2bytes.3.also, you need know the every app memory limits in your device:
3.if the bitmap did not used again, try
recyle()it4.if began the bitmap process, the memory is almost the max memory limit, so use the
adb shell dumpsys meminfo $pidto check the memory usage, also theddms - allocation trackeris useful