Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemory Exception: Will compressing images reduce heap size?

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?

like image 307
dabaerju Avatar asked Nov 13 '22 12:11

dabaerju


1 Answers

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).

like image 190
Joseph Earl Avatar answered Nov 15 '22 05:11

Joseph Earl