title: Reading images using ImageIO.read(file); causes java.lang.OutOfMemoryError: Java heap space
body: <p>I am using a ImageIO API to write a PNG file. This code is called in a loop and causes an OutOfMemory error. Is there anyway the following code can be fixed to avoid the OutOfMemory error? Or is the only option to increase the JVM heap size?</p>
  
  <pre><code>File file = new File(resultMap.get("outputFile").toString());
  
  //ImageIO to convert jpg to png
  BufferedImage img = ImageIO.read(file);
  file = new File(resultMap.get("outputFile").toString() + ".png");
  ImageIO.write(img, "png", file);   
  </code></pre>
  
  <p>Java heap size is 1024M.</p>