简体   繁体   English

Android:使用png而不是矢量图像时分配的内存太多

[英]Android: Too much allocated memory while using png instead of vector images

I am using 10 png images of size 20-30kb in imageView, but the allocated memory increases from 70mb to 270mb when this activity loads. 我在imageView中使用大小为20-30kb的10 png图像,但是当此活动加载时,分配的内存从70mb增加到270mb。 So why this too much of memory is allocated to these images. 那么为什么将太多的内存分配给这些图像呢?

This is the screenshot of memory allocation 这是内存分配的屏幕截图

在此输入图像描述

This is one of my image 这是我的形象之一

在此输入图像描述

File size doesn't matter. 文件大小无关紧要。 No, matter your image is of 20kb but its resolution is quite big. 不,你的图像是20kb,但它的分辨率非常大。 When image is loaded into memory it takes memory equal to totalNoOfDotsInImageBitmap * 4bytes and totalNoOfDotsInImageBitmap = width *height of image. 当图像加载到内存中时,它占用的内存等于totalNoOfDotsInImageBitmap * 4bytes和totalNoOfDotsInImageBitmap = width *图像的高度。

4 bytes - because of ARBG ( 1 byte per argument) for single dot of bitmap 4个字节 - 因为ARBG(每个参数1个字节)用于位图的单个点

so reducing width and height of image may solve your problem. 因此减少图像的宽度和高度可以解决您的问题。

Depending on where you are putting the assets it may be trying to load a file that is too large. 根据您放置资产的位置,可能会尝试加载过大的文件。

For instance, if you have it on drawable or nodpi-drawable a device with a low density will try to load a potentially large image. 例如,如果您将它放在drawablenodpi-drawable上,则具有低密度的设备将尝试加载可能较大的图像。

Also, bear in mind that the actual file size is not that important as it is probably small due to compression, but the image has to be converted to bitmap when it gets drawn, so if the actual size is too much that can also cause an OOM. 另外,请记住,实际的文件大小并不重要,因为压缩可能很小,但是在绘制图像时必须将图像转换为位图,因此如果实际尺寸太大,也会导致OOM。

If you have access to the original I would recommend using a vector drawable (it's a simple shape so should be ok) and AS will generate the required PNG files for older versions. 如果您可以访问原始版本,我建议使用矢量drawable(它是一个简单的形状,所以应该没问题),AS将为旧版本生成所需的PNG文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM