ImageIO memory issue

E

Erik

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
 
K

Knute Johnson

Erik said:
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?
 
E

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?

:) 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);
}
}
 
K

Knute Johnson

Erik said:
:) 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.
 
D

Daniel Pitts

Erik said:
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.
 
A

Andrew Thompson

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.
 
E

Erik

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?


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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top