简体   繁体   中英

android 495KB image taking 10MB of Heap size

I am trying to load an image in my app having size 495KB. If I load this image than heap size increases from 25MB to 35MB which is causing real memory issues in my app. If i dont load this image than heap size stays at 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.

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.

There is some good guidance in the Displaying Bitmaps Efficiently article , as well as quite a few questions here on SO discussing good solutions.

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). 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

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

BitmapFactory will auto-scale your bitmap if you load it and have a high DPI screen resolution. I think on devices with the largest DPI value it gets scaled by 2x in both directions.

This may explain some of the unexpected increase. Try putting your images in folder called drawable-nodpi and they will not be scaled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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