Do I need to free Swing components before letting them get garbage collected?

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 ?


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.

Need Your Help

SQL Server 2000 - View list of sprocs with GRANT EXECUTE for a particular role exclusively?

sql-server execute role grant stored-procedures

There are approx 500 sprocs in my SQLSERVER 2000 database; each sproc has a typical Grant Execute statement similar to the following.

What are some good resources for understanding SNMP MIBs?

snmp mib

I know a little about SNMP, but not enough. I need to develop an application that can read standard SNMP MIBs and read/write the various properties. The network end is no problem, but the actual MI...