Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preloading approx. 10 images (8Mpix) in Java

I want to preload (load into memory) about 10 jpg pictures (size 3264px x 2448px). Each image filesize is ~3MB. I've tried to load them into ArrayList of BufferedImage but I get OutOfMemoryError Exception (Java heap space). How to cope with it?

like image 922
latata Avatar asked Dec 01 '25 06:12

latata


2 Answers

Each image filesize is ~3MB. I've tried to load them into ArrayList of BufferedImage but I get OutOfMemoryError Exception (Java heap space). How to cope with it?

Each compressed image size is 3MB. As you wrote in your title, 3264*2448 is 7 990 272 pixels (about 8Mpix). Using the common ARGB that's 32 bits per pixel, or about 32 MB per picture.

If you want to preload these 10 pictures uncompressed (for example in ten BufferedImage), you'll need 320 MB of memory for these 10 pictures only.

So you'd need to run your java application with more memory.

like image 70
TacticalCoder Avatar answered Dec 03 '25 23:12

TacticalCoder


..any possibility to store image in compressed form?

Sure, load them as byte arrays & keep a reference to each (3 Meg) array. As you require each actual image, stamp it out to an image by wrapping it in a ByteArrayInputStream and use that as the source to ImageIO.read(InputStream).

That would come to around 30 Meg for the 10 byte[], and 321 meg for each realized image.

  1. AFAIU JPEG images cannot have transparency, so a 32 Meg ARGB image would be 24 Meg as RGB.
like image 30
Andrew Thompson Avatar answered Dec 04 '25 00:12

Andrew Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!