JDialogs and Memory Leaks.. PLEASE HELP!

Discussion in 'Java' started by Christine, Apr 6, 2004.

  1. Christine

    Christine Guest

    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");

    }
    }
     
    Christine, Apr 6, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    646
    Big Brian
    Oct 14, 2003
  2. Diego Martins
    Replies:
    4
    Views:
    325
    Diego Martins
    Oct 9, 2006
  3. Replies:
    19
    Views:
    560
    Alf P. Steinbach
    Jan 30, 2008
  4. Marcin Rodzik
    Replies:
    7
    Views:
    2,547
    markspace
    May 11, 2010
  5. Replies:
    4
    Views:
    136
Loading...

Share This Page