Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Swing JFrame always reachable?

Tags:

java

swing

jframe

How come JFrame instances are always reachable within a Swing application? Won't that prevent it from being garbage collected?

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

Calling dispose() doesn't help. It is not even shown.

like image 553
milan Avatar asked Nov 14 '22 01:11

milan


1 Answers

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

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

Basically the idea is that both Window and Frame class has a field called weakThis which is a weakreference to the current frame/window object. Whenever the object needs to be stored(for e.g. in appcontext), the weakThis field is used instead of this. (Weakreferences don't stop gc-ing of objects, in case you didn't knew)

EDIT: Window.getWindows is added in 1.6.

like image 103
Suraj Chandran Avatar answered Dec 19 '22 20:12

Suraj Chandran