简体   繁体   English

android 495KB图像占用10MB的堆大小

[英]android 495KB image taking 10MB of Heap size

I am trying to load an image in my app having size 495KB. 我正在尝试在尺寸为495KB的应用程序中加载图像。 If I load this image than heap size increases from 25MB to 35MB which is causing real memory issues in my app. 如果我加载此映像,则堆大小从25MB增加到35MB,这会导致我的应用程序出现实际内存问题。 If i dont load this image than heap size stays at 25MB. 如果我不加载此图像比堆大小保持在25MB。 Can anybody tell why it is taking so much heap size? 谁能说出为什么要占用这么多堆大小?

Image is below 图片如下 在此处输入图片说明

Code that I am using to load an image is 我用来加载图像的代码是

    InputStream s4 = getResources().openRawResource(R.drawable.parallax_layer4);    
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
    System.gc();
    if(bitmap4 != null) {
        bitmap4.recycle();
    }
    s3 = null;
    System.gc();     

    bitmap4 = bitmap(s4);
    layer4Back = new ImageView(this);
    layer4Back.setImageBitmap(bitmap4);
    layer4Back.setScaleType(ScaleType.FIT_XY);
    parallaxLayout.addView(layer4Back, 3, lp);
    try {
        s4.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
    s4 = null;
    System.gc();

private static Bitmap bitmap(final InputStream is) {
    Bitmap bitmap = null;
    System.gc();
    Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inSampleSize = 1; 

    try {
       // bitmap = BitmapFactory.decodeStream(is);
       bitmap = BitmapFactory.decodeStream(is, null, options);

    } catch (Error e) {
       // TODO: handle exception
       e.printStackTrace();
    }
    return bitmap;
}

Thanks Ayaz Alavi 谢谢阿亚兹·阿拉维

The image file is uncompressed when it's loaded into memory. 图像文件加载到内存后将被解压缩。 At 5600 x 480 pixels and 32 bits per pixel, your image works out as almost exactly 10 MB when uncompressed. 在5600 x 480像素和每像素32位的情况下,未压缩的图像几乎可以精确到10 MB。

My recommendation would be to cut it into smaller sections and only load the sections you need. 我的建议是将其切成较小的部分,然后仅加载您需要的部分。

5,600px * 480px * 4 bytes = 10,752,000 bytes, so this isn't surprising. 5,600px * 480px * 4字节= 10,752,000字节,因此这不足为奇。

There is some good guidance in the Displaying Bitmaps Efficiently article , as well as quite a few questions here on SO discussing good solutions. 在“ 有效显示位图”一文中有一些很好的指导,以及关于SO讨论好的解决方案的许多问题。

Well what did you expect? 那么您期望什么? The image is 5600 x 480 px, now in memory every single pixel takes 32 bits (plz correct me someone). 图像是5600 x 480像素,现在内存中的每个像素都需要32位(请给我指正)。 Do the math and you get a good idea why it is a problem. 算一下,您就会知道为什么会出现问题。 You need to use a smaller image OR cut the image up in parts and load the parts that is needed when they are needed and discard the unnecessary parts. 您需要使用较小的图像,或者将图像切成一部分,并在需要时加载所需的部分,并丢弃不必要的部分。

I had a similar problem which is discussed here: Android app crash out of memory on relaunch 我有一个类似的问题在这里讨论: 重新启动时,Android应用程序崩溃导致内存不足

There is also a long discussion about the subject and possible solutions here: Strange out of memory issue while loading an image to a Bitmap object 这里也有关于主题和可能解决方案的长时间讨论: 将图像加载到Bitmap对象时出现内存不足问题

BitmapFactory will auto-scale your bitmap if you load it and have a high DPI screen resolution. 如果加载位图并且具有高DPI屏幕分辨率,则BitmapFactory将自动缩放位图。 I think on devices with the largest DPI value it gets scaled by 2x in both directions. 我认为,在具有最大DPI值的设备上,它在两个方向上都会缩放2倍。

This may explain some of the unexpected increase. 这可以解释一些意外的增加。 Try putting your images in folder called drawable-nodpi and they will not be scaled. 尝试将图像放在名为drawable-nodpi文件夹drawable-nodpi ,它们将不会缩放。

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

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