Why is Swing JFrame always available?

Why are JFrame instances always available in a Swing app? Doesn't that stop him from collecting garbage?

JFrame frame = new JFrame();
System.out.println(Window.getWindows().length); // says '1'

The challenge dispose()does not help. It is not even shown.

+5
source share
3 answers

There is no getWindows method in java.awt.Windows, but there is a Frame.getFrames () method.

But I know that both of them use WeakReference to manage such things.

, , Window, Frame weakThis, weakreference frame/window. , (, appcontext), weakThis. (Weakreferences gc-ing , )

EDIT: Window.getWindows 1.6.

+5

, , JFrame , , . , null;

0, .

JFrame frame = new JFrame();
frame = null;
System.gc();
System.out.println(Window.getWindows().length);

, , GC. , System.gc() GC. .

-2

Because the JFrame object was not garbage collected. Try the following:

import java.io.*;
import java.awt.*;
import javax.swing.*;
public class WTest
{
    public static void main(String[] args)
    {
        JFrame jfTmp = new JFrame();
        jfTmp.dispose();
        jfTmp = null;

        System.runFinalization();
        System.gc();

        Window[] arrW = Window.getWindows();
        for(int i = 0; i < arrW.length; i ++)
            System.out.println("[" + i + "]\t" + arrW[i].getClass());
    }
}
-2
source

All Articles