• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception with ImageIO.read

 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe ImageReader caches instances, hence the dispose() method.
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe Ess: I am using ImageIO.read; how can I use ImageReader?.
[ September 11, 2008: Message edited by: Alejandro Barrero ]
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe Ess: Thank you for your help. I am going to try it.
[ September 12, 2008: Message edited by: Alejandro Barrero ]
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may also want to invoke flush() on any BufferedImage instances you are done with.
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic