title: Memory shortage problem while using BufferedImage and ImageIO to save images
body: <p>I want to save some java.awt.Image objects into the disk. But when I try like:</p>
  
  <pre><code>for (Image image : images) {
      image = new ImageIcon(image).getImage();
      BufferedImage temp = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
      Graphics2D g = temp.createGraphics();
      g.drawImage(image, 0, 0, null);
      g.dispose();
      File tempFile = null;
      try {
          tempFile = File.createTempFile("img", ".jpg");
          ImageIO.write(temp, "jpg", tempFile);
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      files.add(tempFile);
  }
  </code></pre>
  
  <p>I get java heap space error.</p>
  
  <p>I needed to put</p>
  
  <pre><code>image = new ImageIcon(image).getImage();
  </code></pre>
  
  <p>to get all the pixels loaded so that image.getwidth(null) and image.getHeight(null) return valid results.</p>
  
  <p>Is there any other way to do this except for increasing heap size?</p>