Results 1 to 12 of 12
Like Tree5Likes
  • 3 Post By Fubarable
  • 1 Post By doWhile
  • 1 Post By Tolls

Thread: BufferedImage not going to GC

  1. #1
    vspin is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default BufferedImage not going to GC

    I cannot seem to get a BufferedImage to dispose. Every time I run the code below, the BufferedImage stays in memory until the app is closed.

    Java Code:
    private void test() {
            BufferedImage bi = new BufferedImage(2048, 2048, BufferedImage.TYPE_INT_RGB);
            try {
                bi = ImageIO.read(new File("res/large.png"));
    
                //test.flush();
                //test = null;
            } catch (IOException ex) {
    
            }
        }
    I tried flush, setting to null, and both. Nothing seems to work.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: BufferedImage not going to GC

    How are you determining that the BufferedImage is in memory? As an aside, never let exceptions fall through - at the least print out the Exception message.

  3. #3
    vspin is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedImage not going to GC

    Absolutely.

    I have compared the difference in memory usage when executing the method, and not.

    I even executed this, which produced even greater memory use:

    Java Code:
    private void test() {
            BufferedImage bi = new BufferedImage(2048, 2048, BufferedImage.TYPE_INT_RGB);
            try {
                bi = ImageIO.read(new File("res/large1.png"));
                bi = ImageIO.read(new File("res/large2.png"));
                bi = ImageIO.read(new File("res/large3.png"));
                bi = ImageIO.read(new File("res/large4.png"));
                //test.flush();
                //test = null;
            } catch (IOException ex) {
     
            }
        }
    I think I'm going to use this instead as it doesn't seem to have the same memory issue:

    Java Code:
    Image image = Toolkit.getDefaultToolkit().createImage("res/large.png");

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: BufferedImage not going to GC

    I have compared the difference in memory usage when executing the method, and not.
    How? How do you know the garbage collector is getting called? How do you evaluate the object is in memory? To be more explicit, I'd recommend an SSCCE showing the problem, and a full description as to how you are determining the memory usage.

  5. #5
    vspin is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedImage not going to GC

    Quote Originally Posted by doWhile View Post
    How? How do you know the garbage collector is getting called? How do you evaluate the object is in memory? To be more explicit, I'd recommend an SSCCE showing the problem, and a full description as to how you are determining the memory usage.
    Java Code:
    import java.awt.Image;
    import java.awt.Toolkit;
    import javax.swing.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class Main extends JFrame
    {
        public Main() {
            runTest();
        }
        
        private void runTest(){
            System.out.println("Total memory (bytes) before loading images: " + 
                    Runtime.getRuntime().totalMemory());
            
            loadImagesImageIO();
            System.out.println("Total memory (bytes) after loading images (ImageIO): " + 
                    Runtime.getRuntime().totalMemory());
            
            loadImagesToolkit();
            System.out.println("Total memory (bytes) after loading images (Toolkit): " + 
                    Runtime.getRuntime().totalMemory());
        }
        
        private void loadImagesImageIO(){
            BufferedImage bi = new BufferedImage(2048, 2048, BufferedImage.TYPE_INT_RGB);
            try {
                bi = ImageIO.read(new File("res/large1.png"));
                bi = ImageIO.read(new File("res/large2.png"));
                bi = ImageIO.read(new File("res/large3.png"));
                bi = ImageIO.read(new File("res/large4.png"));
                //test.flush();
                //test = null;
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
        
        private void loadImagesToolkit(){
            Image image = Toolkit.getDefaultToolkit().createImage("res/large1.png");
            image = Toolkit.getDefaultToolkit().createImage("res/large2.png");
            image = Toolkit.getDefaultToolkit().createImage("res/large3.png");
            image = Toolkit.getDefaultToolkit().createImage("res/large4.png");
    
        }
       
       public static void main(String[] args)
       {
          Main main = new Main();
          main.setVisible(true);
       }
    }
    large1.png to large4.png are 2048x2048 PNGs.


    Here's the output I'm getting:

    Total memory (bytes) before loading images: 16252928
    Total memory (bytes) after loading images (Toolkit): 16252928
    Total memory (bytes) after loading images (ImageIO): 84516864


    I changed the order where ImageIO was executed first, and I got:

    Total memory (bytes) before loading images: 16252928
    Total memory (bytes) after loading images (ImageIO): 84516864
    Total memory (bytes) after loading images (Toolkit): 84516864


    Monitoring my memory use with Windows Task Manager, my memory never eventually drops.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: BufferedImage not going to GC

    So what? Why is this an issue? The GC is never guaranteed to collect marked objects and often doesn't unless the application runs low on memory? And your memory monitoring still does not identify what occupies what spot of memory.
    doWhile, DarrylBurke and vspin like this.

  7. #7
    vspin is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedImage not going to GC

    The GC is never guaranteed to collect marked objects and often doesn't unless the application runs low on memory?
    I don't know. As the forum suggests, I'm new to Java. :)

    Okay, well thanks for your help.

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: BufferedImage not going to GC

    Second what Fubarable said. I'd recommend googling terms like 'java garbage collection' for a plethora of resources on garbage collection to give you an idea why you observe what you do, how you can monitor the memory usage of an application, and when you should worry about it.
    vspin likes this.

  9. #9
    vspin is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedImage not going to GC

    Quote Originally Posted by doWhile View Post
    Second what Fubarable said. I'd recommend googling terms like 'java garbage collection' for a plethora of resources on garbage collection to give you an idea why you observe what you do, how you can monitor the memory usage of an application, and when you should worry about it.
    Thanks. I'll look around.

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default Re: BufferedImage not going to GC


  11. #11
    vspin is offline Member
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedImage not going to GC

    Okay, so you guys were right. Hovercraft Full Of Eels @stackoverflow provided me this link:
    Understanding Weak References | Java.net

    Another helpful link I found after reading the one above, was this:

    http://java.sun.com/docs/books/perfo...PAppGC.fm.html

    An object enters an unreachable state when no more strong references to it exist. When an object is unreachable, it is a candidate for collection. Note the wording: Just because an object is a candidate for collection doesn't mean it will be immediately collected. The JVM is free to delay collection until there is an immediate need for the memory being consumed by the object.
    Which is what you guys said. I'm off to skip and lick my lollipop without worry of allocated memory creep.


    Thank you all

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: BufferedImage not going to GC

    Also, you are using totalMemory, which is the amount of memory the OS is providing to the JVM, not the amount of memory used in the heap.
    That would be totalMemory - freeMemory.
    totalMemory rarely shrinks, and is not a garbage collection thing in any case. I'm not entirely sure what mechanism controls the reduction in totalMemory.

    So even if the image was collected you would not actually see that using that method.
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. BufferedImage to ImageIcon
    By han003 in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 11:13 AM
  2. bufferedImage
    By ranadav in forum New To Java
    Replies: 3
    Last Post: 06-05-2010, 03:22 PM
  3. BufferedImage rgb
    By Bill87 in forum New To Java
    Replies: 2
    Last Post: 02-25-2010, 06:31 PM
  4. Using BufferedImage
    By timkd127 in forum New To Java
    Replies: 5
    Last Post: 12-19-2009, 09:17 PM
  5. BufferedImage through FTP
    By dudejonne in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •