tags:

views:

295

answers:

4

Wnen I use external resources such as files or DB connection I need to close them before I let them go.

Do I need to do the same thing with Swing components ? If yes then how ?

A: 

Objects are automatically garbage collected if there are no references to them. You do not have to treat Swing components the same way that you do external resources. However, if you have a component that you are not going to need later you can set any references to that component to null.

JPanel p = new JPanel();
p = null;

Setting p to null does not delete the object but it removes any references to the object so the next time the garbage collector passes it gets picked up.

You will have to be careful though that other reference to the component do not exist.

Vincent Ramdhanie
p = null is rarely needed. See http://stackoverflow.com/questions/271530/cross-references-and-garbage-collection
VonC
But swing components use external resources (eg window handles I gues), what about them ?
lbownik
+1  A: 

At one point it was taught that you had to disconnect all the listeners, because otherwise they'd act as references to the Swing component. But I'm told that this is no longer a problem.

Paul Tomblin
+3  A: 

Normally, you don't need to dispose of objects when you are done with them (although setting the references to them to null may allow them to be GCed sooner). However, AWT and Swing objects allocate some amount of native resources that need to be freed. Furthermore, the AWT thread treats the windows as top-level objects, preventing them from being garbage collected and the JVM from terminating.

Thus, when you are done with your window, you need to dispose of it, which frees the native resources that it has allocated. One way to do this is to call Window.dispose() on it. However, a better option would be to call JFrame.setDefaultCloseOperation() when you initialize each of your root windows. If you pass it DISPOSE_ON_CLOSE it will take care of disposing itself when the user closes the window. When the last such window closes, the AWT thread will stop blocking and allow the JVM to close (assuming you don't have any other aberrant threads running). Alternatively, you can pass it EXIT_ON_CLOSE, which will cause your window to call System.exit(), which tells the JVM that your application is done and to gracefully terminate.

James
An what if I only get rid of a JPanel lith some controls in order to put there someting else ? Withount closing the entire window. What then ?
lbownik
A: 

Windows (including dialogs and frames) should be disposed. If you create (AWT) Graphics objects then they should be disposed of too (but usually locally within, say, a paintComponent method).

If you have a listener to update a short lived target from a long lived source, then you should remove it before discarding the target. There's a neat hack where the listener uses a WeakReference to the target, so that it can deregister when the reference goes away (and an event is fired).

Tom Hawtin - tackline