By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,963 Members | 1,749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,963 IT Pros & Developers. It's quick & easy.

OutOfMemoryError: Java Heap Space

P: 23
I have a program that shows a thumbnail of an image. If the user clicks on the thumbnail a new JFrame is opened that shows the full size image. If the image is larger than the screen, it gets scaled to the the size of the screen.

Functionally, the program works well. However, when testing I found that after clicking the thumbnails of several images (and subsequently closing their viewing frames), I get an java.lang.OutOfMemoryError: Java heap space. I'm not really sure what to do about this. Below the code I've included a snippet of the StackTrace.

I've been searching around, and most forums talk about increasing heap size. I don't want to have to do that. It seems like if I'm properly releasing resources, I shouldn't have the problem. All I want the program to do is: show a full size image in a new window, then return those resources when the window is closed.

Thanks in advance for any help.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. //==================================================================================
  2. //will scale a provided image to the provided size
  3. //==================================================================================
  4.        public static BufferedImage scaleImage (Image image, int width, int height)
  5.       {
  6.          if (image == null)
  7.          {
  8.             return null;
  9.          }
  10.          else
  11.          {
  12.             Image temp = image.getScaledInstance (width, height, Image.SCALE_SMOOTH);
  13.  
  14.             BufferedImage scaledImage = new BufferedImage (
  15.                temp.getWidth (null), temp.getHeight (null), BufferedImage.TYPE_INT_RGB);
  16.  
  17.             Graphics2D g2d = (Graphics2D)scaledImage.getGraphics();
  18.             g2d.drawImage (temp, 0, 0, null);
  19.             g2d.dispose();
  20.  
  21.             return scaledImage;
  22.          }
  23.       }
  24. //==================================================================================
  25. //will open a full size version of an image in a new window
  26. //==================================================================================
  27.        public static void showFullSizeImage (BufferedImage temp, String fileName)
  28.       {
  29.          final BufferedImage scaledImage;
  30.          int width = temp.getWidth();
  31.          int height = temp.getHeight();
  32.          Toolkit tk = Toolkit.getDefaultToolkit();
  33.  
  34.          Dimension screenDimension = tk.getScreenSize();
  35.  
  36.          if (temp != null)
  37.          {
  38.             if (width >= height)
  39.             {
  40.                if (screenDimension.getWidth() >= width)
  41.                {
  42.                   scaledImage = Utility.scaleImage (
  43.                      temp, (int)(screenDimension.getWidth()), -1);
  44.                }
  45.                else
  46.                {
  47.                   scaledImage = temp;
  48.                }
  49.             }
  50.             else
  51.             {
  52.                if (screenDimension.getHeight() >= height)
  53.                {
  54.                   scaledImage = Utility.scaleImage (
  55.                      temp, -1, (int)(screenDimension.getHeight()));
  56.                }
  57.                else
  58.                {
  59.                   scaledImage = temp;
  60.                }
  61.             }
  62.          }
  63.          else
  64.          {
  65.             scaledImage = null;
  66.             JOptionPane pane = new JOptionPane();
  67.             pane.showMessageDialog (null, "Could not display image");
  68.          }
  69.  
  70.          if (scaledImage != null)
  71.          {
  72.             JFrame frame = new JFrame (Utility.parseFileName (fileName));
  73.             frame.addWindowListener (
  74.                    new WindowAdapter(){
  75.                       public void windowClosed (WindowEvent e){
  76.                         JFrame frame = (JFrame)e.getSource();
  77.                         frame.dispose();
  78.                      }
  79.                   });
  80.  
  81.             JPanel panel = 
  82.                 new JPanel(){
  83.                    public void paintComponent (Graphics g){
  84.                      g.drawImage (scaledImage, 0, 0, null);
  85.                      g.dispose();
  86.                   }
  87.                };
  88.             panel.setPreferredSize (
  89.                new Dimension (scaledImage.getWidth(), scaledImage.getHeight()));
  90.  
  91.             frame.add (panel);
  92.             frame.pack();
  93.             frame.setLocationRelativeTo (null);
  94.             frame.setVisible (true);
  95.          }
  96.       }
  97.  
Expand|Select|Wrap|Line Numbers
  1. at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
  2. at java.awt.image.Raster.createPackedRaster(Raster.java:458)
  3. at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
  4. at java.awt.image.BufferedImage.<init>(BufferedImage.java:312)
  5. at Utility.scaleImage(Utility.java:104)
  6. at Utility.showFullSizeImage(Utility.java:174)
  7. at DisplayPanel$2.mouseClicked(DisplayPanel.java:78)
  8.  
Jan 24 '09 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
I think that final BufferedImage scaledImage reference is in your way. Create a separate class extending from a JFrame that you can construct with a BufferedImage parameter so you don't need that final reference anymore.

When you dispose the JFrame, also flush() the image.

kind regards,

Jos
Jan 25 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.