How do I delete an object referenced by a dialog?

后端 未结 8 2454
独厮守ぢ
独厮守ぢ 2021-02-14 11:42

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

8条回答
  •  别跟我提以往
    2021-02-14 12:33

    "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

    1. An object of class TestRun is created and its reference is stored in the variable 'tr'.

    2. Garbage Collector is called. But makes no effect. Because no object is dereferenced yet.

    3. 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.

    4. 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.

提交回复
热议问题