2

Context: I have a Swing JFrame application that is run from a parent "launcher" program. The launcher is responsible for check for updates and for actually updating the app. So it runs the app in a children classloader, eventually relaunching it when a new jar is avaiable.

Now, everything works fine, but i have this problem: it looks like something from the JFrame is leaked and prevents both the JFrame and the whole classloader from being garbage collected. Cycling throu Frame.getFrames() shows that the JFrame is still there somehow, even if it was disposed.

I've been able to reduce the test case to the following class. Note that the call to setBounds is necessary for the bug to happen, if you remove it, the JFrame is garbaged correclty and disappear from getFrames(). I was unable to understand what in the method trigger the problem, as it does millions of things inside.

I'm running on JDK 1.7.0_25-b17

import java.awt.Frame;
import java.io.IOException;

import javax.swing.JFrame;

public class BugTest extends JFrame {
    public BugTest() {
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setBounds(0, 0, 100, 100); // <- comment this and the leak disappear! -<<
        setVisible(true);
    }

    public static void main(String[] args) throws IOException {
        new BugTest();

        System.out.println("Close the window then press enter");
        System.in.read();
        System.gc();

        Frame[] frames = Frame.getFrames();
        System.out.println("There are " + frames.length + " frames (should be 0)");
        for (Frame frame : frames) {
            System.out.println(frame);
        }
    }

    public void finalize() {
        System.out.println("Frame was finalized");
    }
}

BTW it looks like the thread AWT-Windows is impossible to kill. Once started it never terminate even when there are no more windows or anything. Is it possible to properly clean up?

Boes anybody have an insight on what's happening? Am i supposed to do something else to clean everything up?

4

1 回答 1

3
  • 顶级容器基于来自本机操作系统的资源(AWT 资源)

  • 这个生命周期Objects只和JVM一起结束,永远不会被GC'ed,你需要自己做改变

于 2013-07-31T11:50:53.277 回答