-1

I'm using ImageIO.read(InputStream is) in loop for making BufferedImages from Remote Server (HttpConnection and InputStream)

but, It happens out of memory java heap space everytime.

I don't know why this happens.

This is my jvm parameter option.

-verbosegc -XX:+PrintGCDetails -Xmx1024m - XX:PermSize=128m

and full gc logs.

[Full GC [PSYoungGen: 506K->0K(6656K)] [PSOldGen: 963K->1457K(5504K)] 1469K-
>1457K(12160K) [PSPermGen: 8556K->8556K(131072K)], 0.0155080 secs]

[Full GC [PSYoungGen: 480K->0K(11072K)] [PSOldGen: 5001K->4697K(11456K)]
5481K->4697K(22528K) [PSPermGen: 18222K->18222K(131072K)], 0.0350780 
5481K->secs]

as you see there are free space in younggen and oldgen, permgen. That is a last full gc log.

After logging, jvm shutdown with out of memory.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.awt.color.ICC_Profile.getData(ICC_Profile.java:1315)
        at java.awt.color.ICC_Profile.getInstance(ICC_Profile.java:762)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.setImageData(JPEGImageReader.java:610)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:561)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:316)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:438)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:554)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:940)
        at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:924)
        at javax.imageio.ImageIO.read(ImageIO.java:1400)
        at javax.imageio.ImageIO.read(ImageIO.java:1322)

Please help. Thanks.

I'm sorry for my question.

I have posted my source. It extract hashcode from images.

1.main codes.

HashExtractor h = new HashExtractor();
while(rs.next()) {
   Inputstream is = getStream(); 
   h.doSomething(is);
   //something to do using bi.
   is.close();
}

//in HashExtractor
public String doSomething(InputStream is) {
   BufferedImage bi = ImageIO.read(is);
   String hash = "";
   //extract hash.  

   return hash;
}

I have removed some details to looks easy. (getting connection.. etc)

Thanks!

yongseok Jang
  • 97
  • 3
  • 7
  • 3
    *"Please help."* Please ask a question. I can think of 6 questions that might be asked based on your experience. Choose one. You might also want to add the detail of whether the code is keeping references to earlier images or only references one at a time, as well as the size of images in terms of WxH and bit depth. – Andrew Thompson Aug 24 '12 at 02:21
  • 3
    Could you post your relevant section of the code? Or an [SSCCE](http://sscce.org/) version of your code? That would really help us help you! – Sujay Aug 24 '12 at 02:22
  • I have posted my source codes. Thanks! – yongseok Jang Aug 24 '12 at 04:04
  • 2
    This is not a "here's my code so far, now do my job for me" site. – Bohemian Aug 24 '12 at 04:06
  • I didn't ask "do my job for me"... If you have feel like that I apologize.. I think so many lines in this question, I cutted out some lines. – yongseok Jang Aug 24 '12 at 05:22

1 Answers1

3

Instead of all that manipulation of the BufferedImage after you read it, get an ImageReader for it and use an ImageReadParam to set the render size, and whatever else you can persuade it to do while it is reading the image. That way you don't end up with multiple copies of the image in memory.

user207421
  • 305,947
  • 44
  • 307
  • 483