wood burning stoves*
The moose likes I/O and Streams and the fly likes Exception with ImageIO.read Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Exception with ImageIO.read" Watch "Exception with ImageIO.read" New topic
Author

Exception with ImageIO.read

Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
I am getting a the exception


The problem doesn't happen the first or second time I run the code. My code is


I am at the end of my wits. Please help.

Alejandro Barrero


Your help will be greatly appreciated,
Alejandro Barrero
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

You are getting an out of memory error.
This means that your JVM has run out of memory.
Is the image you are reading huge or your JVM heap size is too small?

You can increase the heap size by using -Xmx JVM parameter.
This tip gives some more idea on it.


apigee, a better way to API!
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
The program runs fine two or three times with the same image and then it fails. It is not the image. It appears ImageIO.read is creating objects every time it runs.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Alejandro:
It appears ImageIO.read is creating objects every time it runs.[/QB]


Let me clarify this a little.
Within the same jvm instance, you are reading multiple images?
OR
you launch a new jvm for every image read?

If you are reading multiple images in the same jvm instance and the image size is big then after a few reads you will get the error. Assuming that GC did not clear the image data stored in memory i.e. you are clinging on to the generated image objects.

Analysing the cause of Out of Memory is a complex process.
This article tells a few tools to use. (Although the article is for jdk 6 but many of the tools are available in prior jdk versions also.)

You can also use profilers like JProbe to analyse memory usage.
[ September 10, 2008: Message edited by: Nitesh Kant ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

I believe ImageReader caches instances, hence the dispose() method.


[How To Ask Questions On JavaRanch]
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Thank you Nitesh. You were correct that it is probably a problem with creating and not recovering memory. I finally determined that the problem was happening when I closed the dialog with the x button on the right upper corner; If i close the dialog using dispose() I have no problems. Why dispose() is not executed when I closed the dialog with the x button is beyond me, but the problem is solved.

I am still having problems even using dispose().
[ September 11, 2008: Message edited by: Alejandro Barrero ]
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Joe Ess: I am using ImageIO.read; how can I use ImageReader?.
[ September 11, 2008: Message edited by: Alejandro Barrero ]
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Running on Eclipse with TPTP profiling I got some results that make the problem inexplicable. I am editing images that I read from blobs in a database. The first time I try to edit an image the memory for byte[] jumps to 28,974,344, the second time for the same image 29M, then it fails the third time. TPTP profiler gives 101M of 183M. The error is not an OutOfMemory exception, it is Java heap space.

I changed eclipse.ini to
-Xms256m
-Xmx256m

But I am still having the same problem.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

Originally posted by Alejandro Barrero:
Joe Ess: I am using ImageIO.read; how can I use ImageReader?.


As the JavaDoc for ImageIO states:
A class containing static convenience methods for locating ImageReaders and ImageWriters

So if you use ImageIO, you are using ImageReader.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Joe Ess: Thank you for your help. I am going to try it.
[ September 12, 2008: Message edited by: Alejandro Barrero ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

You may also want to invoke flush() on any BufferedImage instances you are done with.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
I used ImageReader.dispose() and BuferredImage.flush but I still have the problem. Maybe the problem is with other objects that are not being garbage collected, but TPTP profiling din't show anything.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
I thank you all for your help.

I found an amazing answer in another forum (Experts exchange - http://www.experts-exchange.com/) and I think it is valuable to post it here.

The problem you encountered is typically a JAVA problem.
The mechanism that processes images doesn't release the used memory
what ever you do.

Some month ago I submitted almost the same question and the solution
I found myself was to spawn an extra program to do the 'dirty' work
and exit then (to release the memory).
 
Don't get me started about those stupid light bulbs.
 
subject: Exception with ImageIO.read