garbage collector for JFrame's object

后端 未结 3 2051
抹茶落季
抹茶落季 2021-01-23 20:44
import javax.swing.*;

public class Main
{
    public Main()
    {
        JFrame jf = new JFrame(\"Demo\");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          


        
3条回答
  •  情书的邮戳
    2021-01-23 21:09

    Given the invokeLater() call, the call to GC will probably occur 1st1.

    BTW - calling Runtime.gc() is generally pointless, the JRE won't GC till it needs to.

    1. E.G.

    Output

    GC called
    Frame visible
    

    Code

    package test;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class VisibleFrameGC {
    
        VisibleFrameGC() {
            JFrame jf = new JFrame("Demo");
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setSize(100, 100);
            jf.setVisible(true);
            System.out.println("Frame visible");
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new VisibleFrameGC();
                }
            });
            Runtime.getRuntime().gc();
            System.out.println("GC called");
        }
    }
    

提交回复
热议问题