热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

关于位图创建的AndroidOutOfMemory错误-AndroidOutOfMemoryErroronBitmapCreation

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 个解决方案

#1


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 - 分配跟踪器也很有用

#2


1  

If your display width is 480, your bitmap is 470x376. At 4 bytes per pixel that is 706880 bytes. The only way to use less memory is to make the bitmap smaller.

如果您的显示宽度为480,则您的位图为470x376。每像素4个字节,即706880字节。使用更少内存的唯一方法是使位图更小。

#3


1  

Basically recycle your bitmaps and you shouldn't have that problem any more. Use recycle().

基本上回收你的位图,你不应该再有这个问题了。使用recycle()。

#4


1  

This is not really a problem to fix, it's a design issue to address. You cannot assume anything about the heap that will be made available to you. Here are some things that might help you.

这不是一个真正需要修复的问题,这是一个需要解决的设计问题。您不能假设有关可供您使用的堆的任何内容。以下是一些可能对您有所帮助的事情。

  • One thing you might want to consider seriously is loading a compressed version of the Bitmap: Figure out the amount of memory available on the heap before loading the Bitmap. Based on this, compute the size of the Bitmap that you want to load, compute the height and width from this and then load the Bitmap.
  • 您可能需要认真考虑的一件事是加载Bitmap的压缩版本:在加载Bitmap之前计算堆上可用的内存量。在此基础上,计算要加载的位图的大小,从中计算高度和宽度,然后加载位图。
  • Make sure you are not leaking Bitmaps. This is one of the major issues people run into. This might happen in a multitude of ways, up from leaking Views to leaking Activities. To check this out, profile your working memory.
  • 确保您没有泄漏位图。这是人们遇到的主要问题之一。这可能以多种方式发生,从泄漏视图到泄漏活动。要检查这一点,请分析您的工作记忆。
  • When there are no memory leaks, the GC automatically cleans up the Bitmaps. In pre-3.0 devices, you have to invoke recycle() because the GC doesn't do a good job. This is not required in post-3.0 devices. Though be careful, if you invoke recycle() on a Bitmap thats being used, say in an ImageView, you'll get an exception.
  • 当没有内存泄漏时,GC会自动清理位图。在3.0之前的设备中,您必须调用recycle(),因为GC不能很好地完成工作。在3.0之后的设备中不需要这样做。虽然要小心,如果你在一个正在使用的Bitmap上调用recycle(),比如在ImageView中,你会得到一个例外。

#5


0  

You could always try using soft referenced bitmaps however they may get garbage collected faster than you need them to be.

您总是可以尝试使用软引用的位图,但是它们可能会比您需要的更快地收集垃圾。


推荐阅读
  • 微软评估和规划(MAP)的工具包介绍及应用实验手册
    本文介绍了微软评估和规划(MAP)的工具包,该工具包是一个无代理工具,旨在简化和精简通过网络范围内的自动发现和评估IT基础设施在多个方案规划进程。工具包支持库存和使用用于SQL Server和Windows Server迁移评估,以及评估服务器的信息最广泛使用微软的技术。此外,工具包还提供了服务器虚拟化方案,以帮助识别未被充分利用的资源和硬件需要成功巩固服务器使用微软的Hyper - V技术规格。 ... [详细]
  • 解决java.lang.IllegalStateException: ApplicationEventMulticaster not initialized错误的方法和原因
    本文介绍了解决java.lang.IllegalStateException: ApplicationEventMulticaster not initialized错误的方法和原因。其中包括修改包名、解决service name重复、处理jar包冲突和添加maven依赖等解决方案。同时推荐了一个人工智能学习网站,该网站内容通俗易懂,风趣幽默,值得一看。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • EPICS Archiver Appliance存储waveform记录的尝试及资源需求分析
    本文介绍了EPICS Archiver Appliance存储waveform记录的尝试过程,并分析了其所需的资源容量。通过解决错误提示和调整内存大小,成功存储了波形数据。然后,讨论了储存环逐束团信号的意义,以及通过记录多圈的束团信号进行参数分析的可能性。波形数据的存储需求巨大,每天需要近250G,一年需要90T。然而,储存环逐束团信号具有重要意义,可以揭示出每个束团的纵向振荡频率和模式。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文介绍了OpenStack的逻辑概念以及其构成简介,包括了软件开源项目、基础设施资源管理平台、三大核心组件等内容。同时还介绍了Horizon(UI模块)等相关信息。 ... [详细]
  • Android工程师面试准备及设计模式使用场景
    本文介绍了Android工程师面试准备的经验,包括面试流程和重点准备内容。同时,还介绍了建造者模式的使用场景,以及在Android开发中的具体应用。 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • Apache Shiro 身份验证绕过漏洞 (CVE202011989) 详细解析及防范措施
    本文详细解析了Apache Shiro 身份验证绕过漏洞 (CVE202011989) 的原理和影响,并提供了相应的防范措施。Apache Shiro 是一个强大且易用的Java安全框架,常用于执行身份验证、授权、密码和会话管理。在Apache Shiro 1.5.3之前的版本中,与Spring控制器一起使用时,存在特制请求可能导致身份验证绕过的漏洞。本文还介绍了该漏洞的具体细节,并给出了防范该漏洞的建议措施。 ... [详细]
author-avatar
XL
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有