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: -
//==================================================================================
-
//will scale a provided image to the provided size
-
//==================================================================================
-
public static BufferedImage scaleImage (Image image, int width, int height)
-
{
-
if (image == null)
-
{
-
return null;
-
}
-
else
-
{
-
Image temp = image.getScaledInstance (width, height, Image.SCALE_SMOOTH);
-
-
BufferedImage scaledImage = new BufferedImage (
-
temp.getWidth (null), temp.getHeight (null), BufferedImage.TYPE_INT_RGB);
-
-
Graphics2D g2d = (Graphics2D)scaledImage.getGraphics();
-
g2d.drawImage (temp, 0, 0, null);
-
g2d.dispose();
-
-
return scaledImage;
-
}
-
}
-
//==================================================================================
-
//will open a full size version of an image in a new window
-
//==================================================================================
-
public static void showFullSizeImage (BufferedImage temp, String fileName)
-
{
-
final BufferedImage scaledImage;
-
int width = temp.getWidth();
-
int height = temp.getHeight();
-
Toolkit tk = Toolkit.getDefaultToolkit();
-
-
Dimension screenDimension = tk.getScreenSize();
-
-
if (temp != null)
-
{
-
if (width >= height)
-
{
-
if (screenDimension.getWidth() >= width)
-
{
-
scaledImage = Utility.scaleImage (
-
temp, (int)(screenDimension.getWidth()), -1);
-
}
-
else
-
{
-
scaledImage = temp;
-
}
-
}
-
else
-
{
-
if (screenDimension.getHeight() >= height)
-
{
-
scaledImage = Utility.scaleImage (
-
temp, -1, (int)(screenDimension.getHeight()));
-
}
-
else
-
{
-
scaledImage = temp;
-
}
-
}
-
}
-
else
-
{
-
scaledImage = null;
-
JOptionPane pane = new JOptionPane();
-
pane.showMessageDialog (null, "Could not display image");
-
}
-
-
if (scaledImage != null)
-
{
-
JFrame frame = new JFrame (Utility.parseFileName (fileName));
-
frame.addWindowListener (
-
new WindowAdapter(){
-
public void windowClosed (WindowEvent e){
-
JFrame frame = (JFrame)e.getSource();
-
frame.dispose();
-
}
-
});
-
-
JPanel panel =
-
new JPanel(){
-
public void paintComponent (Graphics g){
-
g.drawImage (scaledImage, 0, 0, null);
-
g.dispose();
-
}
-
};
-
panel.setPreferredSize (
-
new Dimension (scaledImage.getWidth(), scaledImage.getHeight()));
-
-
frame.add (panel);
-
frame.pack();
-
frame.setLocationRelativeTo (null);
-
frame.setVisible (true);
-
}
-
}
-
-
at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
-
at java.awt.image.Raster.createPackedRaster(Raster.java:458)
-
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
-
at java.awt.image.BufferedImage.<init>(BufferedImage.java:312)
-
at Utility.scaleImage(Utility.java:104)
-
at Utility.showFullSizeImage(Utility.java:174)
-
at DisplayPanel$2.mouseClicked(DisplayPanel.java:78)
-
1 7125 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
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 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...
|
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
|
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...
|
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..
|
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..
| |
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...
|
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.
|
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);
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |