OutOfMemoryError:位图大小超出 VM 预算:- Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2928002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:55:39  来源:igfitidea点击:

OutOfMemoryError: bitmap size exceeds VM budget :- Android

androidbitmapout-of-memory

提问by Andy

Possible Duplicate:
Android: Strange out of memory issue while loading an image to a Bitmap object

可能的重复:
Android:将图像加载到位图对象时出现奇怪的内存不足问题

i am downloading images from Url and displaying them. At download time it is giving out of memory error : bitmap size exceeds VM budget. I am using drawable. Code is below:

我正在从 Url 下载图像并显示它们。在下载时,它给出了 out of memory error : bitmap size exceeds VM budget. 我正在使用可绘制。代码如下:

HttpClient httpclient= new DefaultHttpClient();
HttpResponse response=(HttpResponse)httpclient.execute(httpRequest);
HttpEntity entity= response.getEntity();
BufferedHttpEntity bufHttpEntity=new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();

Bitmap bm = BitmapFactory.decodeStream(instream);
Bitmap useThisBitmap = 
Bitmap.createScaledBitmap(bm,bm.getWidth(),bm.getHeight(), true);
bm.recycle();
BitmapDrawable bt= new BitmapDrawable(useThisBitmap);
System.gc();

Here is the error:

这是错误:

`05-28 14:55:47.251: ERROR/AndroidRuntime(4188): 
 java.lang.OutOfMemoryError: bitmap size exceeds VM budget`

回答by Sando

Use decodeStream(is, outPadding, opts)with

使用decodeStream(is, outPadding, opts)

BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inDither=false;                     //Disable Dithering mode
opts.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
opts.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
opts.inTempStorage=new byte[32 * 1024]; 

回答by Samuh

You could check the image size and then downsample it by appropriate factor.

您可以检查图像大小,然后通过适当的因子对其进行下采样。

See this question: Handling large Bitmaps

看到这个问题:处理大位图

回答by Sephy

This issue seems to have been reported several times, hereand herefor instance... sorry Shalini but if it's the same issue, it seems that there is no solution at all...

这个问题似乎已经被多次报告过,例如这里这里......对不起Shalini,但如果是同样的问题,似乎根本没有解决方案......

The only advice of Romain Guy is to use less memory...

Romain Guy 的唯一建议是使用更少的内存......

So, good luck to think your stuff differently...

所以,祝你好运,以不同的方式思考你的东西......

回答by Rorist

Finally, after resample the image as suggested above, you may call bitmap_file.recycle().

最后,按照上面的建议重新采样图像后,您可以调用bitmap_file.recycle()

回答by Fran Marzoa

The FACT is that there is a BUG on some Android versions, particularly version 2.1 fails all the time with issues like this one.

事实是,某些 Android 版本存在 BUG,尤其是 2.1 版一直失败并出现此类问题。

I released an app in which I have took a lot of care on resource use. I have even deleted a lot of bitmaps that I were using and now they are created on the fly using graphic primitives. I also recycle bitmaps when no using them. And of course I have checked that I have no memory leaks in my app: the used memory does NOT grow without control, it keeps all the time within reasonable values.

我发布了一个应用程序,在其中我非常注意资源的使用。我什至删除了很多我正在使用的位图,现在它们是使用图形原语动态创建的。不使用位图时,我也会回收它们。当然,我已经检查过我的应用程序中没有内存泄漏:使用的内存不会在不受控制的情况下增长,它始终保持在合理的值内。

Although I have invest a lot of effort on trying to avoid this problem, I continue getting lots of annoying exceptions like that from 2.1 and 2.1-update1 devices. I am using critercism now to report crashes, and I have seen that It occurs even when the app is using just 4 megabytes of RAM, four times less than the 16M of heap size that every Android device must have for an app -and the fact is that most of devices these days have heap sizes bigger than 16M-.

尽管我已投入大量精力试图避免这个问题,但我仍然从 2.1 和 2.1-update1 设备中收到许多令人讨厌的异常。我现在正在使用 critercism 来报告崩溃,我已经看到即使应用程序仅使用 4 MB 的 RAM,它也会发生,比每个 Android 设备必须为应用程序提供的 16M 堆大小少四倍 - 以及事实是现在大多数设备的堆大小都大于 16M-。

All my bitmaps have a size of 800x480 pixels, that on the worst case as ARGB_8888 may not occupy more than 1.5MB each one, but it crashes trying to load one when having just 4 megabytes occupied, so there should be at least another 12 MB free. And most of my Bitmaps are loaded as ARGB_4444 that occupies a half of memory, I only use ARGB_8888 when the bitmaps looks really poor with 4444.

我所有的位图的大小都是 800x480 像素,在最坏的情况下,因为 ARGB_8888 每个可能不会占用超过 1.5MB,但是当只有 4 MB 被占用时它会崩溃尝试加载一个,所以应该至少还有另外 12 MB自由。而且我的大部分位图都加载为占用一半内存的 ARGB_4444,当位图看起来非常糟糕时,我只使用 ARGB_8888 4444。

So for me it is pretty clear that there is something on these Android versions that is not working fine. 99'9% of this crashes comes from 2.1 and 2.1-update, and the rest may be explained by other punctual reasons.

所以对我来说很明显这些 Android 版本上有一些不能正常工作的东西。99'9% 的崩溃来自 2.1 和 2.1 更新,其余的可能由其他准时原因解释。

回答by Thefirstkilla

I've tried many things this is what works.

我已经尝试了很多东西,这是有效的。

BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inDither=false;                     //Disable Dithering mode
opts.inPurgeable=true;        
opts.inScale=8;
///after you use your images
System.gc();

回答by Pervez Alam

This is a practical answer, what I tried to avoid this problem at Run-time. And it also solved my problem.

这是一个实用的答案,我试图在运行时避免这个问题。它也解决了我的问题。

Runtime.getRuntime().gc();

Calling Garbage Collector is a good Idea.

调用垃圾收集器是个好主意。