Why did all my bitmaps grow by 200%?

I have serious memory problems in my application [1]. To explore this, I took the heapdumps of my application in different states. I saw that some bitmap images occupy a huge amount of memory. I wrote a small tool [2] that decodes byte arrays into Windows raster files (.bmp) so that I can see raster images and compare them with the files that I have in my res/drawable .

I found that all my files are doubled twice.
First, I checked the largest of them: a byte array buffer larger than 9 MB in heap, which was decoded as a good 1920x1280 image, while the original one was 960x640 png file.
I tried with the second largest, more than 3 MB, which was once decoded, showed a beautiful 754x1200 picture, the original size was ... guess what? Good file with a diagonal of 377x600.

What gives?

I turned on HW acceleration in my Android manifest file (although I'm not sure I really need it, I just use some basic views and actions).
I am using Android 4.0.2 on a GSM Galaxy Nexus (yakju). I get feedback from my testers that a problem is present on their 4.0.3 Nexus S, although I still could not check their heaps.

I'm trying to save memory here if Android doubles everything, it is not surprising that the application crashes quickly because the heap usage gets too high (about 64 MB in my case). Hope there is a reason and a way around.

Literature:

+6
source share
1 answer

When you put images in res/drawable , Android will assume that they have dpi 160, i.e. this is the same as putting them in res/drawable-mdpi . Galaxy Nexus is an xhdpi device, i.e. It has (generalized) dpi with a resolution of 320. To compensate for the high resolution display, Android will increase the number of images by 200%.

The solution is simple, just put the images in res/drawable-xhdpi . Then the declared dpi will correspond to the display you display, and Android will not perform image scaling.

See http://developer.android.com/guide/practices/screens_support.html for more details.

+8
source

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


All Articles