C
Christine
The class below is called by another class of mine. They double click
on a thumbnail Image, and
I have created a JDialog that shows the picture at full size.
Problem is the memory usage. Everytime I use this class my memory goes
up, after
I open so many pictures I crash with out of memory exception. So many
other people
have had this problem.. but I cant figure out what I am doing wrong.
I would expect the dialog to increase memory by a little when it
opens,
but when it closes it never lets go.. (I am running with java 1.4.2).
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.image.ImageObserver;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ShowFullPicDialog extends JDialog
{
private String imageType;
private String subImageType;
private String date;
private ImagePanel imagePanel = null;
public ShowFullPicDialog(Image image, String imageType,
String subImageType, String date)
{
// close application action listener
this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
closeWindow(e);
}
});
this.setModal(true);
//get the photo and put it in the panel
imagePanel = new ImagePanel(image,
image.getWidth(null),
image.getHeight(null));
JScrollPane scroll = new JScrollPane(imagePanel);
this.getContentPane().add(scroll);
Toolkit theToolkit = this.getToolkit();
Dimension windowSize = theToolkit.getScreenSize();
if((image.getWidth(null) > windowSize.getWidth()) ||
(image.getHeight(null) > windowSize.getHeight()))
{
this.setSize(new Dimension(new Double(windowSize.
getWidth() * 0.8).intValue(),
new Double(windowSize.getHeight() *
0.8).intValue()));
scroll.setPreferredSize(new
Dimension(image.getWidth(null),
image.getHeight(null)));
imagePanel.setPreferredSize(new Dimension(image.
getWidth(null),
image.getHeight(null)));
}
else
{
this.setSize(new Dimension(image.getWidth(null) +
14, image.getHeight(null) + 33));
}
this.setTitle(imageType + " : " + subImageType +
" Date: " + date);
this.setLocation(
(int)(windowSize.getWidth() / 2 -
this.getWidth() / 2),
(int)(windowSize.getHeight() / 2 -
this.getHeight() / 2));
this.show();
image.flush();
image=null;
theToolkit=null;
scroll=null;
windowSize=null;
this.removeAll();
}
/******************************************************************
* Close Application event handler
* @param e the close application event
***************************************************************/
private void closeWindow(WindowEvent e)
{
imagePanel.setImage(null);
imageType=null;
subImageType=null;
date=null;
imagePanel = null;
this.removeAll();
this.getGraphics().dispose();
this.setVisible(false);
this.dispose();
System.gc();
System.out.println("exit dialog window");
}
}
on a thumbnail Image, and
I have created a JDialog that shows the picture at full size.
Problem is the memory usage. Everytime I use this class my memory goes
up, after
I open so many pictures I crash with out of memory exception. So many
other people
have had this problem.. but I cant figure out what I am doing wrong.
I would expect the dialog to increase memory by a little when it
opens,
but when it closes it never lets go.. (I am running with java 1.4.2).
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.image.ImageObserver;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ShowFullPicDialog extends JDialog
{
private String imageType;
private String subImageType;
private String date;
private ImagePanel imagePanel = null;
public ShowFullPicDialog(Image image, String imageType,
String subImageType, String date)
{
// close application action listener
this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
closeWindow(e);
}
});
this.setModal(true);
//get the photo and put it in the panel
imagePanel = new ImagePanel(image,
image.getWidth(null),
image.getHeight(null));
JScrollPane scroll = new JScrollPane(imagePanel);
this.getContentPane().add(scroll);
Toolkit theToolkit = this.getToolkit();
Dimension windowSize = theToolkit.getScreenSize();
if((image.getWidth(null) > windowSize.getWidth()) ||
(image.getHeight(null) > windowSize.getHeight()))
{
this.setSize(new Dimension(new Double(windowSize.
getWidth() * 0.8).intValue(),
new Double(windowSize.getHeight() *
0.8).intValue()));
scroll.setPreferredSize(new
Dimension(image.getWidth(null),
image.getHeight(null)));
imagePanel.setPreferredSize(new Dimension(image.
getWidth(null),
image.getHeight(null)));
}
else
{
this.setSize(new Dimension(image.getWidth(null) +
14, image.getHeight(null) + 33));
}
this.setTitle(imageType + " : " + subImageType +
" Date: " + date);
this.setLocation(
(int)(windowSize.getWidth() / 2 -
this.getWidth() / 2),
(int)(windowSize.getHeight() / 2 -
this.getHeight() / 2));
this.show();
image.flush();
image=null;
theToolkit=null;
scroll=null;
windowSize=null;
this.removeAll();
}
/******************************************************************
* Close Application event handler
* @param e the close application event
***************************************************************/
private void closeWindow(WindowEvent e)
{
imagePanel.setImage(null);
imageType=null;
subImageType=null;
date=null;
imagePanel = null;
this.removeAll();
this.getGraphics().dispose();
this.setVisible(false);
this.dispose();
System.gc();
System.out.println("exit dialog window");
}
}