2

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 ?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Łukasz Bownik
  • 6,149
  • 12
  • 42
  • 60

4 Answers4

5

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
  • 2,050
  • 13
  • 15
  • 1
    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 ? – Łukasz Bownik Nov 26 '08 at 14:21
  • 1
    OP has an unanswered comment here that I'd like to know about as well. Do JPanels require cleanup? – snickers10m Jul 25 '15 at 00:22
2

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
  • 179,021
  • 58
  • 319
  • 408
  • 1
    It depends. If your component dies at the same time as or consistently after the component you registered the listener with, nothing special is needed, an unreachable cycle of references is automatically GC'd. However, if your component may be closed/discarded with the component or service you listen to still alive, your listener will become a memory leak, and may prevent GC of your view, model, and any other objects kept alive by the live references from that listener. It is important to de-register listeners to services or components that have longer lifetimes than your own. – Theodore Murdock Aug 09 '16 at 20:28
0

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
  • 102,349
  • 23
  • 137
  • 192
0

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
  • 145,806
  • 30
  • 211
  • 305