A huge surge in memory consumption when using png with a lot of transparent area

I use the button with png background for the CopyToClipboard function in my application (used 6-7 times in a different fragment). Since the image should be small for my purpose, I enlarged the image area by adding an additional transparent area around the image to increase the size of the button for the click area but keep the image small (I know that it is inefficient, and since then I have developed a better way to achieve of this).

I noticed huge bursts later after loading other images (large images about 150 kb in size), and after a lot of debugging (and I mean a lot!), I found that the problem is not related to the bigger one but due to the CopyToClipboard image having a size of only 8 KB !!! Changing the old CopyToClipboard image (with a smaller transparent area) led to normal memory consumption.

My question is , why did this happen ? For such a small image , to create such huge bursts (more than doubled the memory consumption from the previous one) and made the application slow is rather difficult.

Image shown below: The white area is a transparent area. My button size: 15dp x 15dp.

enter image description here

I repeat, My question is Why did this happen? Not a solution for it, since I already solved the problem.

+1
source share
1 answer

Actually, it doesnโ€™t matter that your disk is only 7-8 KB, because when decoding it will take up much more memory.

Apparently, a large transparent area can be effectively encoded in a PNG file, so the image has this small size. But in fact, its size is (600 x 745), so in memory it will take about (600 * 745 * 4) bytes plus some meta-information, so almost 2 megabytes. 4 means the number of bytes needed to encode the color with the alpha channel. Android bitmaps are presented inside a linear one-dimensional array of integers, so you can imagine that the system needs to allocate an array of size 600 * 745 = 447000 to create your bitmap.

That is why the memory consumption is so great for such a simple image.

+1
source

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


All Articles