views:

553

answers:

3

I'm writing game with a large amount of .png pictures. All worked fine. Than I added new activity with WebView and got memory shortage. After that I made some experiment - replace game .png images with ones that just fully filled with some color. As result memory shortage had gone.

But I suppose that Bitmap internally hold each pixel separately so such changes should have no effect. Maybe this because of initial images have alpha channel and my test images have not it?

But actually question is: Will decreasing .png images files sizes make some effect on decreasing usage of VM application heap or not?

+1  A: 

Android or not, a bitmaps in memory aren't compressed, so they are going to take (bits per pixel) * width * height, with a bit of variation depending on the pixel format.

I don't know the details of how PNGs are drawn, but probably what's happening is that simpler PNGs take less memory to decode.

Seth
A: 

Memory usage of a Bitmap object in Android is related to the image resolution, not the original format (jpg, png, etc...) or filesize. It needs about 3 bytes pr pixel (1 byte pr color channel).

Anyway, if you are using BitmapFactory to decode a image, a smaller source-file requires less memory when decoding the inputstream.

You can test this yourself by using the Dalvik debugger (ddms). Goto the Sysinfo tab an select Memory usage from the dropdown. You will see how much memory your app i using.

PHP_Jedi
+2  A: 

You should look into the bitmap configuration you're decoding your images into. I don't know specifically what the configuration files mean, but for example, you can decode into ARGB_8888 or simply RGB_565. RGB_565 uses less memory, presumably because it doesn't have an alpha (transparency) channel and uses less bits for each colour. In your case what's problably happening is that the simple images are being decoded into RGB_565 whereas the more complicated ones were decoded into ARGB_8888.

The way to change which configuration is being used is during the decoding of 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 that and see if it helps. Doing that certainly helped me with my game.

Steve H
Yes, when I made my test I used BitmapFactory.decodeResource() without options, so one color images can decode to RGB_565 and my game images to ARGB_8888. I'm not thinking about this before but today when I will come to work I make second test with one options ARGB_8888 for all and see what happens.Unfortunately almost all my game's images require transparency so I can't just use RGB_565, now I use ARGB_4444
nahab
It seems size of .png files somehow have influence to bitmap in memory. Maybe I will make deeper investigation next week
nahab