How do I delete an object referenced by a dialog?

后端 未结 8 2439
独厮守ぢ
独厮守ぢ 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:22

    No need to delete the object. The garbage collector will take care of the memory as soon as it is no longer referenced.

    0 讨论(0)
  • 2021-02-14 12:26

    You meant "how" to destroy it? There is no way to destroy an object explicitly in Java. garbage collector in Java automatically reclaims the memory occupied by it If there is no reference of the same exists.

    "but I create dialog with jpanel inside it, and that jpanel will be still referrenced. I want to destroy that dialog when click my own button "Cancel"

    Just try setting that JPanel object to null or call the dispose method on it if that is available.

    0 讨论(0)
  • 2021-02-14 12:30

    If you want to delete the assign that object reference to null, so that when Garbage Collector runs for the next time it can destroy the object thinking it is not getting referenced.

    0 讨论(0)
  • 2021-02-14 12:33

    There is no need to destroy object in Java in the way like in C++.There is garbage collector which destroys(release memory used by) objects automatically after there is no references to this object in running code. Everything that you can do is to force destroy link by Object obj = null; This kills reference to obj.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-14 12:34

    You can override the finalize() method (see http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize%28%29) to perform cleanup when an object is destroyed.

    However, unlike C++ there is no guarantee when will this method get called. In C++ you have stack-stored objects which are destroyed when execution leaves the scope in which they were defined.

    In Java all object are stored on the heap. They will be finalized when the Garbage collector decides to collect them (implies that they are not reachable from your app) but you don't know when will the GC kick in. Thus, if you have some cleanup that must take place at a certain point (e.g., closing a file so that it can be written to) you have to code it yourself and not rely on the finalize() method being called.

    The typical pattern for doing that is based on a try ... finally block:

    X x = null;
    try {
      // ... do some stuff
      x = ... // obtain an object
       ... // do some stuff 
    }
    finally {
      if(x != null)
         x.close(); // Perform cleanup WRT x
    }
    

    (Admittedly, ugly)

    0 讨论(0)
提交回复
热议问题