Reputation: 738
I made a JFrame (call it J1
) that includes a JButton. When the button is clicked, a new JFrame (call it J2
) opens.
J1
also has an ArrayList that contains J2
, and all other Jframes that were opened.
J2
is set to DISPOSE_ON_CLOSED
I set for J2
a windowClosed() method. For testing, inside the JFrame I looped threw the ArrayList
, until I get to the current J2
that was just closed, and surprisingly when I do J2.setVisisble(true)
the J2
window returns!
I also checked threw the Task Manager, and saw the though opening a new J2
, will make the overall program consume more RAM, closing each J2
doesn't show much difference on the Task Manager, it doesn't look like any memory was freed. It looks like that the memory consumption is going back to "normal" after a few seconds, so i doubt that is has anything to do directly with the J2
.
I tried printing (System.out.print
) all the ArrayList
content every time a new J2
is initiated, and after opening a window, closing it, and opening a new one, I get the following messgae:
home.ATMmachine[frame0,252,198,620x420,invalid,hidden,layout=java.awt.BorderLayout,title=ATM Machine No.1,resizable,normal,defaultCloseOperation=DISPOSE_ON_CLOSE,rootPane=javax.swing.JRootPane[,9,38,602x373,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
This means that the J2
wasn't completely deleted!
The JFrame dispose()
method is suppose to
Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.
So am I just wrong, and the frame is being deleted sum time later.
If not, then how can i delete the frame in such a way that referencing to it would be like referencing to null?
Upvotes: 1
Views: 4431
Reputation: 285450
I made a JFrame (call it J1) that includes a JButton. When the button is clicked, a new JFrame (call it J2) opens. J1 also has an ArrayList that contains J2, and all other Jframes that were opened. J2 is set to DISPOSE_ON_CLOSED I set for J2 a windowClosed() method. For testing, inside the JFrame I looped threw the ArrayList, until I get to the current J2 that was just closed, and surprisingly when I do J2.setVisisble(true) the J2 window returns!
Closing the JFrame and disposing does not destroy the JFrame object, but rather it releases system resources, the resources needed to display the window on your OS. This is entirely different from the object itself. When you call setVisible(true)
on a disposed JFrame, the resources are re-created, and the window is re-displayed. No objects are created or destroyed by this.
I also checked threw the Task Manager, and saw the though opening a new J2, will make the overall program consume more RAM, closing each J2 doesn't show much difference on the Task Manager, it doesn't look like any memory was freed. It looks like that the memory consumption is going back to "normal" after a few seconds, so i doubt that is has anything to do directly with the J2.
No surprise there.
I tried printing (System.out.print) all the ArrayList content every time a new J2 is initiated, and after opening a window, closing it, and opening a new one, I get the following messgae:
home.ATMmachine[frame0,252,198,620x420,invalid,hidden,layout=java.awt.BorderLayout,title=ATM Machine No.1,resizable,normal,defaultCloseOperation=DISPOSE_ON_CLOSE,rootPane=javax.swing.JRootPane[,9,38,602x373,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
This means that the J2 wasn't completely deleted!
Again, you're confusing objects, which will exist as long as a reference to the object exists, and resources.
The JFrame dispose() method is suppose to Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.
Exactly.
So am I just wrong, and the frame is being deleted sum time later.
An object can only be GC'd when there are no more strong references to it, and the garbage collector only does this when it feels the need to do so, such as when memory is about to run out.
If not, then how can i delete the frame in such a way that referencing to it would be like referencing to null?
Just like any other object. You could create it locally for one, but be careful since Swing listeners have a way to create strong references that you would assume should be weak, and thus make objects persist for longer than you'd like.
On a side note -- why all the JFrames? Most programs I've used that throw a lot of windows at you seem to be quite annoying, which I guess is why I usually only see them created in this way by newbie programmers. Why not swap views instead with a CardLayout?
Please check out: The Use of Multiple JFrames, Good/Bad Practice? for more on this
Upvotes: 5
Reputation: 999
The frame j2 is still referenced in the ArrayList by a pointer , so the garbage collector can't dispose of it yet ... try deleting the pointer from the arraylist, then even if j2 is still there in RAM , it wouldnt have any reference in your program and so garbage collector should remove it at next pass , or if u invoke system.gc() .
Upvotes: 0