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
"System.gc()" is the best way.
gc-Garbage Collector.
This is how you are gonna destroy the object in the program itself
Sample Code:
public class TestRun
{
public static void main(String args[])
{
/*1*/ TestRun tr=new TestRun();
/*2*/ System.gc(); //You can omit this step. This is just an example.
/*3*/ tr=null;
/*4*/ System.gc();
}
}
Explanation
An object of class TestRun is created and its reference is stored in the variable 'tr'.
Garbage Collector is called. But makes no effect. Because no object is dereferenced yet.
The object that was created in step-1, is now dereferenced.But the space that was occupied by the object is still blocked by it.
Garbage Collector is again invoked, and now it sees that the object that was created in step-1 is now dereferenced. Hence, now it frees the space occupied by the object and the object is now deleted from the memory in simple language.
In fact it will delete all those objects that are already dereferenced.
It's good practise to keep calling it in your code.