I created a dialog with a jpanel inside it, and that jpanel will be still referenced if I get rid of the dialog. I want to destroy that dialog and everything in it when I click
If it is a Window and if it is visible
I. frame.setVisible(false);
II. frame.dispose();
II. set reference to null (ex.. frame=null;)
If it is not a Window
I.set reference to null (ex.. x=null;)
That is all , once object be free GC will free the resources when it is like.
Here are somethings you must understand
*. You as Java programmer can not force Garbage collection in Java; it will only trigger if JVM thinks it needs a garbage collection based on Java heap size.
*. There are methods like System.gc () and Runtime.gc () which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen.
*. If there is no memory space for creating new object in Heap Java Virtual Machine throws OutOfMemoryError or java.lang.OutOfMemoryError heap space
And search about this...
J2SE 5(Java 2 Standard Edition) adds a new feature called Ergonomics . goal of ergonomics is to provide good performance from the JVM with minimum of command line tuning.
Answering the question(s) you posed in the comment:
Once you have displayed a dialog:
setVisible(true);
you hide it by calling:
setVisible(false);
and then you must call:
dialog.dispose();
to make sure that all native GUI resources the dialog used get freed. Once you have done this, the garbage collector will clean up all of the objects once you no longer have any references to them.