This discussion is archived
4 Replies Latest reply: Mar 19, 2013 2:40 AM by gimbal2 RSS

JFrame garbage collect not working on Mac

997029 Newbie
Currently Being Moderated
Here is a nice and short program
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

public class TestMemoryFrameClose {

     public static void runOnce(final boolean tearDown, final int sleepTime) {

          try {
               Thread t = new Thread(new Runnable() {

                    public void run() {
                         final JFrame jf = new JFrame("");
                         jf.addWindowListener(
                                   new WindowAdapter() 
                                   {
                                        public void windowClosing(WindowEvent e) {
                                             jf.getContentPane().removeAll();
                                             jf.dispose();
                                        }
                                   });  
                         jf.pack();
                         jf.setVisible(true);
                         try { Thread.sleep(sleepTime); } catch(Exception e) {}
                         if(tearDown) {
                              jf.dispatchEvent(new WindowEvent(jf, WindowEvent.WINDOW_CLOSING));
                         }
                    }

               });
               t.start();
               t.join();
          }
          catch(Exception e)  {
               e.printStackTrace();
          }

     }

     public static void main(String args[]) {
          final boolean tearDown = true;  // Change here
          final int sleepTime = 3000;
          try { Thread.sleep(5000); } catch(Exception e) {}
          for(int i = 0 ; i < 20 ; i++) {
               System.out.println("NR "+i);
               runOnce(tearDown, sleepTime);
          }
          for(int i = 0 ; i < 300 ; i++) {
               try { Thread.sleep(1000); } catch(Exception e) {}
               System.out.println("After - NR "+i);
          }

     }

}
Here is how to use it:

Firstly we shall see an interresting sideeffect.

Compile it and run it as is along with some diagnosis tool. I used jvisualvm. 20 frames comes up and get removed programmatically one by one. When the 300 seconds tick at the end you have plenty of time to see that the number of instances of javax.swing.JFrame go down from 20 to zero or so. They can rest a while but with a click or two from jvisualvm in my case, to execute the garbage collector the number quickly goes down. Interrupt the program.

Secondly we shall see the problem.

Change the tearDown to false. Compile it and run with the diagnosis tool of your choice. The 20 frames comes up but they are not programmatically removed. Remove them by hand as they appear (one by one). Now the 20 instances of javax.swing.JFrame do not get freed away by the GC.

I use java 1.7.0_12-ea-b08, MacOS up to date. Program is launched from Eclipse. On windows-8 this problem does not exist. There they get correctly removed by the GC in both methods. I have not tried this on Linux.

One more thing. The thing with ".getContentPane().removeAll();" mentioned in another thread not improve the behaviour on Mac. Have not tried on w8 or linux.

Edited by: 994026 on 2013-mar-15 00:56

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points