Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ImageIO memory issue

Reply
Thread Tools

ImageIO memory issue

 
 
Erik
Guest
Posts: n/a
 
      05-08-2008
I have a problem with the ImageIO.read(new File(...)); method. When I
call this method and it reads an image,
but it is not stored in a BufferedImage (or something else), it still
consumes memory. So when only reading
a image and not storing the results, the image is still kept inside
the memory. Also forcing to garbage collect does
not help. Does anyone knows how to release (flush) the ImageIO memory?

Erik
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      05-08-2008
Erik wrote:
> I have a problem with the ImageIO.read(new File(...)); method. When I
> call this method and it reads an image,
> but it is not stored in a BufferedImage (or something else), it still
> consumes memory. So when only reading
> a image and not storing the results, the image is still kept inside
> the memory. Also forcing to garbage collect does
> not help. Does anyone knows how to release (flush) the ImageIO memory?
>
> Erik


Are you sure that it is out of scope when you try to garbage collect it?

Just for my curiosity, why would you read an image file and not keep a
reference?

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
 
 
 
Erik
Guest
Posts: n/a
 
      05-08-2008
On 8 mei, 17:51, Knute Johnson <nos...@rabbitbrush.frazmtn.com> wrote:
> Erik wrote:
> > I have a problem with the ImageIO.read(new File(...)); method. When I
> > call this method and it reads an image,
> > but it is not stored in a BufferedImage (or something else), it still
> > consumes memory. So when only reading
> > a image and not storing the results, the image is still kept inside
> > the memory. Also forcing to garbage collect does
> > not help. Does anyone knows how to release (flush) the ImageIO memory?

>
> > Erik

>
> Are you sure that it is out of scope when you try to garbage collect it?
>
> Just for my curiosity, why would you read an image file and not keep a
> reference?
>
> --
>
> Knute Johnson
> email s/nospam/linux/
>
> --
> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
> ------->>>>>>http://www.NewsDemon.com<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


No, I do want to keep a reference, but I tried to Isolate the
memory problem.
When I run this code and I load 2 JPEG images (1.88 MB each), the
program uses 43 MB of
my memory. Why doesn't the memory get released?

Here is some of my code (where I load the images):

/**
*
*/
package thumbnail;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;



/**
* @author Erik Gast
*
*/
public class Thumbnail {
private Image smallThumbnail = null;
private Image normalThumbnail = null;


public Thumbnail(File imageFile, int fileID){
try {
ImageIO.setUseCache(false);
ImageIO.read(imageFile);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public Image getSmallThumbnail(){
return smallThumbnail;
}

public Image getNormalThumbnail(){
return normalThumbnail;
}

public int getWidthNormalThumbnail(ImageObserver obs){
return normalThumbnail.getWidth(obs);
}

public int getHeightNormalThumbnail(ImageObserver obs){
return normalThumbnail.getHeight(obs);
}

public int getWidthSmallThumbnail(ImageObserver obs){
return smallThumbnail.getWidth(obs);
}

public int getHeightSmallThumbnail(ImageObserver obs){
return smallThumbnail.getHeight(obs);
}
}
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      05-08-2008
Erik wrote:
> On 8 mei, 17:51, Knute Johnson <nos...@rabbitbrush.frazmtn.com> wrote:
>> Erik wrote:
>>> I have a problem with the ImageIO.read(new File(...)); method. When I
>>> call this method and it reads an image,
>>> but it is not stored in a BufferedImage (or something else), it still
>>> consumes memory. So when only reading
>>> a image and not storing the results, the image is still kept inside
>>> the memory. Also forcing to garbage collect does
>>> not help. Does anyone knows how to release (flush) the ImageIO memory?
>>> Erik

>> Are you sure that it is out of scope when you try to garbage collect it?
>>
>> Just for my curiosity, why would you read an image file and not keep a
>> reference?
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/linux/
>>
>> --
>> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
>> ------->>>>>>http://www.NewsDemon.com<<<<<<------
>> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

>
> No, I do want to keep a reference, but I tried to Isolate the
> memory problem.
> When I run this code and I load 2 JPEG images (1.88 MB each), the
> program uses 43 MB of
> my memory. Why doesn't the memory get released?
>
> Here is some of my code (where I load the images):
>
> /**
> *
> */
> package thumbnail;
>
> import java.awt.*;
> import java.awt.geom.AffineTransform;
> import java.awt.image.*;
> import java.io.File;
> import java.io.IOException;
>
> import javax.imageio.ImageIO;
>
>
>
> /**
> * @author Erik Gast
> *
> */
> public class Thumbnail {
> private Image smallThumbnail = null;
> private Image normalThumbnail = null;
>
>
> public Thumbnail(File imageFile, int fileID){
> try {
> ImageIO.setUseCache(false);
> ImageIO.read(imageFile);
>
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
>
> public Image getSmallThumbnail(){
> return smallThumbnail;
> }
>
> public Image getNormalThumbnail(){
> return normalThumbnail;
> }
>
> public int getWidthNormalThumbnail(ImageObserver obs){
> return normalThumbnail.getWidth(obs);
> }
>
> public int getHeightNormalThumbnail(ImageObserver obs){
> return normalThumbnail.getHeight(obs);
> }
>
> public int getWidthSmallThumbnail(ImageObserver obs){
> return smallThumbnail.getWidth(obs);
> }
>
> public int getHeightSmallThumbnail(ImageObserver obs){
> return smallThumbnail.getHeight(obs);
> }
> }


When you read a file with ImageIO.read() it creates a BufferedImage. If
your image files are 1.88MB compressed in JPEG format they will be much
bigger when stored as a BufferedImage. Assuming they are color images
using a normal ColorSpace then they could take 4 bytes per pixel. If
your images are 2000x2000 pixels you are looking at 16MB. Big images
eat a lot of memory.

As to why it isn't being garbage collected, I can't tell from just this
code. Why don't you try making an SSCCE that demonstrates your problem.

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      05-09-2008
Erik wrote:
> I have a problem with the ImageIO.read(new File(...)); method. When I
> call this method and it reads an image,
> but it is not stored in a BufferedImage (or something else), it still
> consumes memory. So when only reading
> a image and not storing the results, the image is still kept inside
> the memory. Also forcing to garbage collect does
> not help. Does anyone knows how to release (flush) the ImageIO memory?
>
> Erik

Just because your java programs memory footprint doesn't decrease
doesn't mean that the memory is still "consumed". Java uses garbage
collection, which may wait an indeterminate amount of time before
"freeing" the memory... Unless you are getting OutOfMemoryErrors, don't
worry about your memory footprint.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-09-2008
On May 9, 1:21*am, Erik <erikg...@gmail.com> wrote:
> I have a problem with the ImageIO.read(new File(...)); method.

..
> ...the image is still kept inside
> the memory.


Yes. Java is terribly 'cachey' with images.

The only way around it that I could find was to
read the images as bytes and stamp the bytes out
to an Image as needed. That way, (without a direct
URL or File for the image) the JVM will not store
it internally.

--
Andrew T.
PhySci.org
 
Reply With Quote
 
Erik
Guest
Posts: n/a
 
      05-09-2008
On 9 mei, 07:12, Lew <l...@lewscanon.com> wrote:
> Erik wrote:
> >> When I run this code and I load 2 JPEG images (1.88 MB each), the
> >> program uses 43 MB of my memory.
> >> Why doesn't the memory get released?

> Knute Johnson wrote:
> > As to why it isn't being garbage collected, I can't tell from just this
> > code. Why don't you try making an SSCCE that demonstrates your problem.

>
> What evidence do you have that the memory is not GCed, Erik?
>
> Is this inferred from a '-verbose:gc' output? Would you be so kind as to
> copy-and-post the relevant entry/-ies?
>
> > Also forcing to garbage collect does not help.

>
> Not surprising, since there exists no way to force the JVM to garbage collect,
> save flood-creating objects to fill the heap.
>
> --
> Lew


When profiling the program using eclipse u can force to garbage
collect.
When I do this, the memory still doesn't get released. Does anyone has
an
idea how to release the memory?

Erik
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Crash in Java 1.6.0_13 ImageIO PNG decoder (and possibly later versions) loading large interlaced PNGs with low memory dy/dx Java 16 11-13-2012 02:42 PM
ImageIO on in-memory byte array roger.books Java 6 10-20-2006 06:52 PM
reading large jpeg / jpg files error on java imageio read: javax.imageio.IIOException: Unsupported Image Type Davidski Java 0 11-05-2004 09:44 PM
Potential memory leak using BufferedImage and ImageIO in a servlet? Richard Corfield Java 0 03-04-2004 10:52 AM
javax.imageio.ImageIO.write() failed to find a png writer F C Java 2 10-01-2003 11:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57