473,698 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OutOfMemoryErro r: Java Heap Space

23 New Member
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.OutOf MemoryError: 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
1 7123
JosAH
11,448 Recognized Expert MVP
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

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

Similar topics

0
3032
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 thrown the java process is not exiting.I am using 1.1.8 JRE to startup the RMIServer. What should I do now?I know that the OutOfMemoryError cannot be handled by try/catch block. My 2 concerns are 1)How shall I exit this process 2)Why not the process...
0
6809
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 the issue is. Thanks Ravi
2
4268
bhing
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 shutdown, we dont want our website to be like this, it should always be up....please help... this is the error message s found in catalina.log *************************************************** Mar 21, 2007 10:01:44 AM...
0
2556
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 java.lang.outofMemoryerror: java heap space,i tried by changing the java heap size in jboss. but still am getting the error. can anybody help me out!!! its urgent!!!! thank you..
0
1398
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 java.lang.outofMemoryerror: java heap space,i tried by changing the java heap size in jboss. but still am getting the error. can anybody help me out!!! its urgent!!!! thank you..
5
14985
blazedaces
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 and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
6
2060
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. My code have sql statement and my database table has 60000 rows(record) and keep increasing.
8
7441
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 = conn.prepareStatement(sql); ResultSet results=stmt.executeQuery(); while(results.next()){ String clickurl = results.getString(2);
8
28367
blazedaces
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 (correct me if I'm wrong about this) that can write excel 2007 files. The program works. I've tested it out on very small files (I will include the commented out main method that you can use to test this out yourself if you'd like, that is assuming you...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.