Reducing the size of .png files has some effect for the given Bitmap in memory

I am writing a game with a lot of PNG images. Everything worked fine. Than I added new activity with WebView and got out of memory. After that, I did some experiment - replace PNG images with images that are just completely filled with some color. As a result, the lack of memory is gone.

But I believe that Bitmap internally holds each pixel separately, so such changes should not have any effect. Perhaps this is due to the initial images has an alpha channel, and my test images aren't they?

But the real question is: will PNG file size reduce any effect on reducing the use of the VM application heap or not?

+4
source share
3 answers

You should look into the configuration of the bitmap into which you decode your images. I do not know specifically what the configuration files mean, but for example you can decode in ARGB_8888 or just RGB_565. RGB_565 uses less memory, apparently because it does not have an alpha (transparency) channel and uses fewer bits for each color. In your case, what probably happens is that simple images are decoded in RGB_565, while more complex ones were decoded in ARGB_8888.

The configuration change method is used when decoding your image files as follows:

 BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon, options); 

Experiment with this and see if that helps. It certainly helped me in my game.

+4
source

Android or not, bitmaps in memory are not compressed, so they will occupy (bit per pixel) * width * height with a slight change depending on the pixel format.

I don't know the details of how PNGs are drawn, but it is likely that simpler PNGs require less memory to decode.

+3
source

The memory usage of the Bitmap object in Android is related to the resolution of the image, not the original format (jpg, png, etc.) or file size. This requires approximately 3 bytes of pr-pixel (1-byte color color channel).

In any case, if you use BitmapFactory to decode the image, the smaller source file requires less memory when decoding the input stream.

You can verify this yourself using the Dalvik debugger (ddms). Go to the Sysinfo tab and select "Memory Usage" from the drop-down menu. You will see how much memory your application is using.

+1
source

Source: https://habr.com/ru/post/1306389/


All Articles