JDK-6568965 : Modals Dialogs are never garbage collected
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-06-13
  • Updated: 2010-04-04
  • Resolved: 2007-06-13
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Windows XP Pro version 2002 SP2

A DESCRIPTION OF THE PROBLEM :
I create multiples modals dialogs form a modal dialog. When i close them, they are still reference by the Dialog#modalDialogs field. So they are never garbage collected(and the memory grows).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :

Step
    1. Launch the class TestDialog
    2. Click 4 Times on 'Launch Dlg' button.This will open 4 Dialogs
    3. Close these 4 dialogs
    4. Click on 'GC' button

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Remove All On Dlg Test 3
Remove All On Dlg Test 2
Remove All On Dlg Test 1
Remove All On Dlg Test 0
mem = 576K / 16320K

ACTUAL -
Remove All On Dlg Test 3
Remove All On Dlg Test 2
Remove All On Dlg Test 1
Remove All On Dlg Test 0
mem = 576K / 16320K
Dlg Test 2
Dlg Test 0
Dlg Test 1

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.Box;
import java.awt.HeadlessException;
import java.awt.Window;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

public class TestDialog extends JFrame {
   public static int count = 0;
   public static int dlgcount = 0;
   private WeakHashMap<JDialog,Integer> m_hash = new WeakHashMap<JDialog, Integer>();

   public TestDialog() throws HeadlessException {
      super("Test");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Box b = Box.createHorizontalBox();
      b.add(new JButton(new AShowDlg<JFrame>(this)));
      b.add(new JButton(new AGC()));
      getContentPane().add(b);
      pack();
      setVisible(true);
   }

   private class AGC extends AbstractAction {
      public AGC() {
         super("GC");
      }

      public void actionPerformed(ActionEvent e) {
         System.gc();
         System.gc();
         System.gc();
         System.gc();
         long totMem = Runtime.getRuntime().totalMemory();
         long freeMem = Runtime.getRuntime().freeMemory();
         String  mem = "" + (totMem - freeMem) / 1024 + "K / " + totMem / 1024 + "K";
         System.out.println("mem = " + mem);
         for (JDialog dlg : m_hash.keySet())
            System.out.println(""+dlg.getTitle());

      }
   }

   private class AShowDlg<T extends Window> extends AbstractAction {
      private WeakReference<T> m_dlg;

      public AShowDlg(T dlg) {
         super("Launch Dlg");
         m_dlg = new WeakReference<T>(dlg);
      }

      public void actionPerformed(ActionEvent e) {
         dlgcount++;
         JDialog dlg = new JDialog(m_dlg.get(), "Dlg Test " + (count++), Dialog.ModalityType.APPLICATION_MODAL);
         dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
         dlg.setLocation(0,50*dlgcount);
         Box b = Box.createHorizontalBox();
         b.add(new JButton(new AShowDlg<JDialog>(dlg)));
         b.add(new JButton(new AGC()));
         dlg.getContentPane().add(b);
         dlg.pack();
         m_hash.put(dlg,count-1);
         dlg.setVisible(true);
         dlg.dispose();
         System.out.println("Remove All On "+dlg.getTitle());
         dlg.removeAll();
         dlgcount--;
      }
   }

   private static void createAndShowGUI() {
      JFrame.setDefaultLookAndFeelDecorated(true);
      new TestDialog();
   }

   public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}

---------- END SOURCE ----------