In an attempt to split a large image file, the program fails with the OutOfMemoryError. Further investigation revealed that this problem appears to be coming from the save routine (see below). When I expanded the upper limits of the JVM from the command line for example -Xmx1024m , the program runs a little longer before generating the same error. Unfortunately I never have enough memory to complete the splitting of the image file.
My question is how do I release/dispose the memory of a renderedImage or better still how do I make sure that the memory is released after each image is saved. I have tried System.gc but that has not solved my propblem. Any ideas will be greatly appreciated. By the way I am relatively new to Java so forgive me if this is appears to be a newbie question.
private void save(final RenderedImage image, final String filename)
throws FileAccessException
{
try
{
File saveFile = new File(filename);
ImageOutputStream out= ImageIO.createImageOutputStream(saveFile);
this.imageWriter.setOutput(out);
this.imageWriter.prepareWriteSequence(null);
IIOMetadata metadata = getMetadata(this.imageWriter, image, null);
IIOImage iioImage = new IIOImage(image, null, metadata);
this.imageWriter.writeToSequence(iioImage, this.imageWriterParam);
//
this.imageWriter.endWriteSequence();
//
out.close();
}
catch(IOException ioException)
{
throw new FileAccessException("Can not save the image file", ioException, "Contact system administrator");
}
}
My question is how do I release/dispose the memory of a renderedImage or better still how do I make sure that the memory is released after each image is saved. I have tried System.gc but that has not solved my propblem. Any ideas will be greatly appreciated. By the way I am relatively new to Java so forgive me if this is appears to be a newbie question.
private void save(final RenderedImage image, final String filename)
throws FileAccessException
{
try
{
File saveFile = new File(filename);
ImageOutputStream out= ImageIO.createImageOutputStream(saveFile);
this.imageWriter.setOutput(out);
this.imageWriter.prepareWriteSequence(null);
IIOMetadata metadata = getMetadata(this.imageWriter, image, null);
IIOImage iioImage = new IIOImage(image, null, metadata);
this.imageWriter.writeToSequence(iioImage, this.imageWriterParam);
//
this.imageWriter.endWriteSequence();
//
out.close();
}
catch(IOException ioException)
{
throw new FileAccessException("Can not save the image file", ioException, "Contact system administrator");
}
}