位图创作的Andr​​oid内存不足错误位图、错误、内存不足、oid

2023-09-07 00:03:22 作者:瘾

我收到错误 java.lang.OutOfMemoryError:位图大小超过VM预算

此创建位图手工绘制线图的目的时,会发生。

 宽度= display.getWidth() -  10;
高度=宽度×4/5;
位图emptyBmap = Bitmap.createBitmap(宽度,高度,Config.ARGB_8888);
位图charty = createMyGraphAndStuff(emptyBmap);
 

看起来分配的总内存大约为700 KB,不合理的金额。

我见过从文件创建位图时调用其他的解决方案,但在这里我生成一个自己。我怎样才能尽量减少其内存占用?

这里的一些code,给你一个更好地了解它在做什么:的

 公共位图DrawTheGraphAndStuff(位图位图的String []得分)
{
        位图输出= Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Config.ARGB_8888);
        帆布油画=新的Canvas(输出);

        drawTheGridLines(画布);

        plotTheDataPoints(帆布,得分,称号,0);

        canvas.drawBitmap(位图,矩形,矩形,油漆);

        返回输出;
}
 

解决方案

在OOM发生,给一些建议:

1.need知道OOM发生位置,日志信息就足够了。

2,大部分时间是位图的过程,所以你需要知道多少关于使用内存镜像:

公式: W * H *字节的每一个像素令牌内存,如果配置 Config.ARGB_8888 ,每个像素令牌内存 4字节,如果是 Config.RGB_565 2字节

3.also,你需要知道你的设备的每一个应用程序的内存限制:

  ActivityManager activityManager =(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
   activityManager.getMemoryClass(); //返回以MB为单位的内存大小限制
 

3,如果该位没有再使用,尽量 recyle()

4,如果开始了位图的过程中,内存几乎最大内存限制,所以使用亚行外壳dumpsys meminfo中$ PID 来检查内存使用量,也 DDMS - 分配跟踪是非常有用的。

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 the Config is Config.ARGB_8888, every pixel token memory is 4bytes, if it is the Config.RGB_565, is 2bytes.

3.also, you need know the every app memory limits in your device:

   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

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