473,378 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

OutOfMemoryError: Java Heap Space

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);
  14.             BufferedImage scaledImage = new BufferedImage (
  15.                temp.getWidth (null), temp.getHeight (null), BufferedImage.TYPE_INT_RGB);
  17.             Graphics2D g2d = (Graphics2D)scaledImage.getGraphics();
  18.             g2d.drawImage (temp, 0, 0, null);
  19.             g2d.dispose();
  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();
  34.          Dimension screenDimension = tk.getScreenSize();
  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.          }
  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.                   });
  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()));
  91.             frame.add (panel);
  92.             frame.pack();
  93.             frame.setLocationRelativeTo (null);
  94.             frame.setVisible (true);
  95.          }
  96.       }
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)
Jan 24 '09 #1
1 7101
11,448 Expert 8TB
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,

Jan 25 '09 #2

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

Similar topics

by: Nagaraj | last post by:
I have a RMIServer which reads a XML file during the startup after registering with the rmi registry.When the XML file is too huge I get the java.lang.OutOfMemoryError.But after this error is...
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
by: bhing | last post by:
Hiya!!! Please help me on this, we have a website running under linux with JSP and Apache-tomcat ... when we try to upload and edit something it returns out of memory, and suddenly tomcat will...
by: subashinicse | last post by:
hi everybody, am working with J2EE &hibernate... while i run my application,am sending an object(filetype of size more than 8MB) from jboss to securityserver,where am getting ERROR as...
by: subashinicse | last post by:
hi everybody, am working with J2EE &hibernate... while i run my application,am sending an object(filetype of size more than 8MB) from jboss to securityserver,where am getting ERROR as...
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
by: nickyeng | last post by:
I keep getting this error: java.lang.OutOfMemoryError: Java heap space what possible reason that cause this error ? My boss dont want increase java memory, so i had to change my code. ...
by: jamborta | last post by:
im trying to iterate through a large database (over 5000 entries), and printing out the entries using this code: String sql = "SELECT "+column+ " ,clickurl from " + tableName; stmt =...
by: blazedaces | last post by:
So I have a program below which writes an excel file with multiple sheets based on inputs of sheet names, data, cell types, etc. It uses Apache POI, which is currently the only thing I found...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.