ImageIO memory issue

Discussion in 'Java' started by Erik, May 8, 2008.

  1. Erik

    Erik Guest

    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
     
    Erik, May 8, 2008
    #1
    1. Advertising

  2. 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
     
    Knute Johnson, May 8, 2008
    #2
    1. Advertising

  3. Erik

    Erik Guest

    On 8 mei, 17:51, Knute Johnson <> 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);
    }
    }
     
    Erik, May 8, 2008
    #3
  4. Erik wrote:
    > On 8 mei, 17:51, Knute Johnson <> 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
     
    Knute Johnson, May 8, 2008
    #4
  5. Erik

    Daniel Pitts Guest

    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/>
     
    Daniel Pitts, May 9, 2008
    #5
  6. On May 9, 1:21 am, Erik <> 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
     
    Andrew Thompson, May 9, 2008
    #6
  7. Erik

    Erik Guest

    On 9 mei, 07:12, Lew <> 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
     
    Erik, May 9, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. F C
    Replies:
    2
    Views:
    5,219
  2. Peter the Swede

    ImageIO, error when trying gif

    Peter the Swede, Nov 13, 2003, in forum: Java
    Replies:
    2
    Views:
    3,201
    Marco Schmidt
    Nov 13, 2003
  3. Richard Corfield
    Replies:
    0
    Views:
    3,585
    Richard Corfield
    Mar 4, 2004
  4. Davidski
    Replies:
    0
    Views:
    3,950
    Davidski
    Nov 5, 2004
  5. roger.books

    ImageIO on in-memory byte array

    roger.books, Oct 19, 2006, in forum: Java
    Replies:
    6
    Views:
    922
Loading...

Share This Page