开发者

How do I delete an object referenced by a dialog?

开发者 https://www.devze.com 2022-12-22 18:38 出处:网络
I created a dialog with a jpanel i开发者_JAVA技巧nside 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 a cancel

I created a dialog with a jpanel i开发者_JAVA技巧nside 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 a cancel button. How do I delete both the dialog and the jpanel?


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.


  1. 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;)

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


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


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)


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.


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.


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.


"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

精彩评论

暂无评论...
验证码 换一张
取 消