Image conversion consumes large amount of memory in heap while loading/saving the image

try (
                        OutputStream outputStream = new FileOutputStream(System.currentTimeMillis()+"");
                        Image image = Image.load(inputStream)
                ) {
                    image.setBufferSizeHint(4096);


                    image.save(outputStream, this.outputImageOptions);
                    response=ImageConverter.outStreamToInStream(outStream);
                    LOGGER.log(Level.SEVERE,"after FILE conversion and before dispose");
                    printHeapData();
                    image.dispose();
                }

i tried converting 17 mb jpeg file to bmp/png. output file size is around 300 mb. But memory spikes around 1-1.5 gb during the conversion which lead to OOM during same kind of parallel conversions
andre.jpg (746.1 KB)

Hello @abi1120001128
We appreciate your interest in Aspose.Imaging.
We will check this issue ASAP

Best wishes

1 Like

Hello, @abi1120001128

We have checked your file using the code

try (
		OutputStream outputStream = new FileOutputStream(System.currentTimeMillis()+".png");
		Image image = Image.load("andre.jpg")) 
{
	image.save(outputStream, new PngOptions());
}

According to our observations, the output file is 11 MB for BMP and 8 MB for PNG.
The memory consumption is in the range of 80-90 MB.

There is a method call in your code

response=ImageConverter.outStreamToInStream(outStream);

Perhaps the memory problem is there. We do not recommend using ByteArrayInputStream and ByteArrayOutputStream to read and write images, as they make a huge pressure on the Garbage Collector and consume a lot of extra memory.
It is better to use a code like:

String outFile = System.currentTimeMillis()+".png";
try (Image image = Image.load("andre.jpg"))
{
	image.save(outFile, new PngOptions());
}

// if you need use the output image, you can do
try (InputStream inputS = new FileInputStream(outFile))
{
	// do something.
}

// or
byte[] outData = Files.readAllBytes(Paths.get(outFile));
// do something.

This code will load less memory and run faster.

Please let us know if there is anything else we can help you with.


Best wishes