MsxCowboy
MsxCowboy

Reputation: 41

Swing memory leak on setSize/Bounds and AWT/Swing cleanup

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?

Upvotes: 2

Views: 140

Answers (1)

mKorbel
mKorbel

Reputation: 109813

  • Top-Level Containers are based on resources from Native OS (AWT resurces)

  • life cycle of this Objects ended only together with JVM, and never will be GC'ed, you need to make the changes on your side

Upvotes: 3

Related Questions