Why so much memory?

隐身守侯 提交于 2019-12-03 08:52:41

To answer your first question:

1000*1500*32/8=6000000

(32 bits/pixel for color information)

To answer your second question: you need to reduce the size of the image, either by processing it in chunks, or be reducing the resolution or color depth.

hpique

cdonner put me in the right direction.

Turns out that the original bitmap was using ARGB_8888, which requires 32 bits per pixel and is more than what was needed for this particular app.

Changing the bitmap config to RGB_565, which requires 16 bits per pixel, reduced memory consumption by half.

There is a tricky workaround which I used to avoid OutOfMemoryError. I registered a receiver so that it ran on different process:

<receiver android:name=".ImageTransformReceiver"
             android:exported="true" android:process=":imageTransformProcess"/>

Inside a receiver I do expensive memory operations (loading of two large images and merging them into one). I write the result image into file and send broadcast back to main process referring to the result image file path in Intent.

This is just a hack that allows you to use more OS memory inside one application. Hope this helps.

As of API level, 11 BitmapFactory.Options has a boolean 'inMutable' which can be set to produce mutable Bitmaps.

While this doesn't change the memory use for the individual bitmap, it will save you from having to store two copies in memory.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!