5

I've been meaning to ask this for quite awhile now. I am creating this game where I draw a very large background. But the problem is (of course), when I put more elements to the game, I get an OutOfMemory Exception.

What I've been meaning to ask is, will compressing the image reduce heap size allocation? For example, my PNG background (3000 by 2000 in pixels) is around 1.5 MB. After a series of PNG compressions (Through softwares such as TinyPNG and PNGGauntlet), the size of the background was drastically reduced to 712 KB. The compressed image here is still the same size as the original (3000 by 2000).

Will the heap size allocation for the original background size (1.5 MB) be the same as the compressed (712 KB) one?

Oldskool
  • 34,211
  • 7
  • 53
  • 66
dabaerju
  • 103
  • 1
  • 13
  • 1
    Very nice question. The answer is probably "yes", because graphics systems need uncompressed images to draw on screen, but I am not sure. I would try shrinking the background to, say, 1500 by 1000, and see if the memory consumption drops four times. – Sergey Kalinichenko Jan 25 '13 at 10:44
  • Yes, I will try that as soon as I am finished with the other modules. I'm not sure how to go about this, but after doing image compressions, the FPS of the game has drastically increased. However, I'm not entirely sure if heap size has been reduced. – dabaerju Jan 25 '13 at 10:50

1 Answers1

1

The answer is yes, both the compressed and uncompressed image occupy the same amount of memory - they ultimately just end up as a grid of R,G,B(,A) values.
The only time you could save some memory is if you are using a form of compression that the GPU directly supports (e.g. S3TC compression for OpenGL textures).

Compression reduces two things: the file-size on disk, and the amount of IO time needed to load the file (though this saving may be offset by the decompression process).

You could however look at loading the image into a bitmap type other than ARGB_8888 (e.g. RGB_565) -- this will reduce the amount of memory needed to store the bitmap (but the quality of the image will be reduced).

Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
  • I was very puzzled as to why I kept getting an OutOfMemory Exception even after compressing these images. As far as this goes, I now know that compression only improves performance on screen as well as reduce the time it takes to load. Thank you very much for clearing that up! I guess there's no way other than to allow large heap size or reduce the file size. – dabaerju Jan 25 '13 at 14:39
  • 1
    You could load the image into a bitmap type other than ARGB_8888 (e.g. RGB_565 - this would use half as much memory). – Joseph Earl Jan 25 '13 at 20:18