Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default OutOfMemory Error when read in pictures

    Hi everyone,

    when I try to read in a set of pictures, the first 5 are successfully read in, but after that I have met this error.

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    	at java.awt.image.DataBufferByte.<init>(Unknown Source)
    	at java.awt.image.ComponentSampleModel.createDataBuffer(Unknown Source)
    	at java.awt.image.Raster.createWritableRaster(Unknown Source)
    	at javax.imageio.ImageTypeSpecifier.createBufferedImage(Unknown Source)
    	at javax.imageio.ImageReader.getDestination(Unknown Source)
    	at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
    	at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
    	at javax.imageio.ImageIO.read(Unknown Source)
    	at javax.imageio.ImageIO.read(Unknown Source)
    	at Test.OrdinaryPage.addBiler(OrdinaryPage.java:62)
    	at Test.OrdinaryPage.init(OrdinaryPage.java:95)
    	at Test.SelectorPage.init(SelectorPage.java:42)
    	at Test.SelectorPanel.generateSelectorPages(SelectorPanel.java:46)
    	at Test.StartActionListener.actionPerformed(StartActionListener.java:33)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    my code snippet for reading in image is as follows:

    Java Code:
    BufferedImage myPicture = ImageIO.read(new File(bilderPath));
    ImageIcon imageIcon = new ImageIcon(myPicture); 
    Image image = imageIcon.getImage(); 
    Image newimg = image.getScaledInstance(700, 500,  java.awt.Image.SCALE_SMOOTH); 
    imageIcon = new ImageIcon(newimg);  
    JLabel picLabel = new JLabel(imageIcon);
    This seems to be a out of memory problem. After struggling for hours, I failed to find a solution. Can some shed some light on it? Besides, the number is pictures is really not so large, only 10

    Thank you very much for your time and intelligence.

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    980
    Rep Power
    2

    Default Re: OutOfMemory Error when read in pictures

    Well, out of memory is out of memory. So make the images smaller in size, or increase the memory limit of your program by adding -Xmx1g to the starting Java command line to give the heap 1GB (or any size which works for you).
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: OutOfMemory Error when read in pictures

    The default heap size is very small on some systems. On older versions of OS X, I think it was only 64 MB...
    Get in the habit of using standard Java naming conventions!

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,762
    Rep Power
    5

    Default Re: OutOfMemory Error when read in pictures

    I have a feeling that it is unrelated to the image sizes. It appears you are doing this in response to an action event. Can you show some more of the code you are using to read in the images?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,076
    Rep Power
    6

    Default Re: OutOfMemory Error when read in pictures

    Quote Originally Posted by jim829 View Post
    I have a feeling that it is unrelated to the image sizes.

    Regards,
    Jim
    I am, because of that scaling code that is in there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,762
    Rep Power
    5

    Default Re: OutOfMemory Error when read in pictures

    Well, you are correct! I ran a test in the CLI on Windows.

    Default memory - approx 7 loads before blowing up
    java -Xmx512m - approx 14
    java -Xmx1g - - loaded all 30.

    I just allocated an array to hold them all.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,076
    Rep Power
    6

    Default Re: OutOfMemory Error when read in pictures

    Indeed. It is probably a good idea to store the images scaled in stead of scaling them in the code; that will already save a boat-load of memory to begin with. Just to note: the scaling method returns a NEW image, so the original still exists for the life time of that

    Java Code:
    Image image
    reference.
    Last edited by gimbal2; 02-13-2014 at 03:51 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,762
    Rep Power
    5

    Default Re: OutOfMemory Error when read in pictures

    In the supplied snippet from the OP it wasn't obvious to me what was consuming the extra memory. I still am uncertain but I believe it's related to the image producer. I wrote what I believe the OP wanted to accomplish. However, if I did something wrong or missed something, please let me know. It would still be more efficient to store these in scaled form. In the following code I get no out of memory exceptions with a max heap size of 256mb.

    Java Code:
    JLabel[] picLabel = new JLabel[30];
    for (int j = 0; j < 30; j++) {
     try {
        // read in and scale image
        BufferedImage origImage = ImageIO.read(new File(file));
        Image scaledImage = origImage.getScaledInstance(700,500, BufferedImage.SCALE_SMOOTH);
        
        // creat new buf sized to scaled image
        BufferedImage scaledBuf = new BufferedImage(700,500,BufferedImage.TYPE_INT_RGB);
        
        // transfer scaled image to scaledbuf
        Graphics2D gbuf = (Graphics2D) scaledBuf.getGraphics();
        gbuf.drawImage(scaledImage, 0, 0, null);
        
        // create and store the label
        ImageIcon imageIcon = new ImageIcon(scaledBuf);
        picLabel[j] = new JLabel(imageIcon);
     }
     catch (Exception e) {
        e.printStackTrace();
     }
    Regards,
    Jim
    Last edited by jim829; 02-13-2014 at 10:18 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: OutOfMemory Error when read in pictures

    Thank you SurfMan,
    I have tried to enlarge the size of JVM. By the way do you know any method by which I can output JVM memory I have used when I run the Application.

    For example, I want
    Load 100th pic, check JVM memory

    do something else:

    Load 101th pic, check JVM memory

    because the problem is still not solved. I have to release the resource properly maybe.

  10. #10
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: OutOfMemory Error when read in pictures

    thank you very much, gimbal! by your suggestions :)

  11. #11
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: OutOfMemory Error when read in pictures

    thank you Jim:)
    By the way do you know any method by which I can output JVM memory I have used when I run the Application.

    For example, I want
    Load 100th pic, check JVM memory

    do something else:

    Load 101th pic, check JVM memory

    because the problem is still not solved. I have to release the resource properly maybe.

  12. #12
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    980
    Rep Power
    2

    Default Re: OutOfMemory Error when read in pictures

    The Runtime class has some info about memory: freeMemory(), maxMemory(), totalMemory().

    Runtime (Java Platform SE 7 )
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,762
    Rep Power
    5

    Default Re: OutOfMemory Error when read in pictures

    Check the Runtime class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: OutOfMemory Error when read in pictures

    Sounds like a job for Home — Project Kenai
    Get in the habit of using standard Java naming conventions!

  15. #15
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: OutOfMemory Error when read in pictures

    Thanks Jim :)

  16. #16
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: OutOfMemory Error when read in pictures

    Quote Originally Posted by SurfMan View Post
    The Runtime class has some info about memory: freeMemory(), maxMemory(), totalMemory().

    Runtime (Java Platform SE 7 )
    Hi SurfMan,
    Thank you for your answer. That helps a lot.
    I have tried to debug it with freeMemory() , but the behaviour of the JVM seems to be very mysteries. Actually I use System.out.println(Runtime.getRuntime().freeMemory ()) to output the "free memory" of JVM before and after the picture loading to check if I have handeled the resource releasing properly.


    When the program is first lauched.
    Java Code:
    Starting Memory: 
    15527336
    
    do something......
    
    Before adding Pic:  9965248
    After adding pic:  13076768
    
    ....
    
    Before adding Pic:  13076768
    After adding pic:  31391216
    
    ...
    
    Before adding Pic:  60222000
    After adding pic:  24789552
    
    similar process in between about 40 periods
    
    Finally,
    
    Before adding Pic:  36220648
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    	at java.awt.image.DataBufferByte.<init>(Unknown Source)
    	at java.awt.image.ComponentSampleModel.createDataBuffer(Unknown Source)
    	at java.awt.image.Raster.createWritableRaster(Unknown Source)
    	at javax.imageio.ImageTypeSpecifier.createBufferedImage(Unknown Source)
    	at javax.imageio.ImageReader.getDestination(Unknown Source)
    	at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
    	at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
    	at javax.imageio.ImageIO.read(Unknown Source)
    	at javax.imageio.ImageIO.read(Unknown Source)
    	at Test.OrdinaryPage.addBiler(OrdinaryPage.java:64)
    	at Test.OrdinaryPage.dynamicLoad(OrdinaryPage.java:90)
    	at Test.QuizPageForwardActionListener.actionPerformed(QuizPageForwardActionListener.java:26)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    There is still large free memory (even larger than initial free memory) before loading the picture and loading procedure just like previous repeats is conducted, And why it fails ?
    I think, you can maybe get some clues from this, because my Java knowledge is so shallow that I fail to follow this memory information. By the way, why the starting free memory is so small. I have set JVM memory to 1G through Xmx1g

    Thank you very much :)
    Last edited by tianchen; 02-17-2014 at 10:41 PM.

  17. #17
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: OutOfMemory Error when read in pictures

    You should print the totalMemory() value also. The JVM allocates memory lazily, up to the amount you specify as the max. So the "free" memory you are printing is not max - used, it's allocated - used. I suspect what you will see is the total (allocated) memory steadily rising each time you load an image. You get the out-of-memory error after total reaches max.

    To fix this, you need to find the reference that is keeping these images reachable. For that, you need a heap analysis tool like VisualVM or Eclipse MAT.
    Get in the habit of using standard Java naming conventions!

  18. #18
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,762
    Rep Power
    5

    Default Re: OutOfMemory Error when read in pictures

    How many images are you loading? When I lowered my heap to 64m I was able to load 30 images scaled from (2500 x 2000) to (700 x 500). When I set the heap at 256m I loaded over 100 similar images. I am using the method I posted above.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #19
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: OutOfMemory Error when read in pictures

    Thank for your answer Kjkrum,

    I have tried Eclipse MAT, but it seems to give information, which is not detailed enough in my case. So now I am trying JConsole.

  20. #20
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: OutOfMemory Error when read in pictures

    Hi Jim,
    Thank you for your answer. I am working ón a Project which needs to show patients pictures. There are in all 2000 ,each of which is about 6M. The pictures should be loaded in groups.

    I want to solve this with maximal JVM heap size 1G. So obviously I can not load them once into JVM, so I want to dynamically load them respectively when patient go to that group.And the patient should be able to move forward and backward through these pictures. So theoretically my program should be able to load infinite number of pictures.

    I am using Jconsole, but it fails to connect. It reports:
    Java Code:
    java.lang.SecurityException: Expecting a javax.rmi.ssl.SslRMIClientSocketFactory RMI client socket factory in stub!
        at javax.management.remote.rmi.RMIConnector.checkStub(RMIConnector.java:1881)
        at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:295)
        at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:268)
        at sun.tools.jconsole.ProxyClient.tryConnect(ProxyClient.java:357)
        at sun.tools.jconsole.ProxyClient.connect(ProxyClient.java:313)
        at sun.tools.jconsole.VMPanel$2.run(VMPanel.java:292)

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 04-25-2013, 06:47 PM
  2. Replies: 22
    Last Post: 07-25-2012, 09:51 PM
  3. Replies: 1
    Last Post: 01-20-2010, 04:52 PM
  4. Replies: 7
    Last Post: 01-30-2009, 06:40 AM
  5. Replies: 16
    Last Post: 08-05-2008, 11:34 AM

Posting Permissions

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