0

I have an object called CounterGUI. It creates a GUI. I create it in another class, say MyProgram.

Once MyProgram creates a CounterGUI object, how do I delete the object? Say if I do this:

CounterGUI first = new CounterGUI(); //displays the GUI
first = null;

I thought setting the object to null would delete everything (including the GUI), but it doesn't. How do I completely delete it?

I don't see how this is a duplicate question, the previous answers were to set it to null or a new object, but that doesn't work here.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Exastify
  • 9
  • 1
  • 3

4 Answers4

1

Assuming that CounterGUI is a java.awt.Window, then:

  • The way to make the window disappear is to call setVisible(false).

  • The way to disconnect it entirely from the native windowing is to call dispose().

  • Once the Window has been disposed, making it unreachable (e.g. by assigning null to all of your variables that reference it) will make it eligible for garbage collection.

Simply assigning null while the Window is visible will have no effect. The a visible Window object is connected to various things that prevent it from being garbage collected.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • [Top-Level Containers havent finalize()](http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime), then dispose or setVisible is the same – mKorbel May 15 '13 at 09:57
1

Java uses automatic garbage collection. When you set an object to null and it is the last reference remaining, the JVM will reclaim the memory used.

In your case, however, it may be that a thread is created (with GUIs this is often the case) and a reference to the current object is given to it. In this case, setting the reference to null has no effect as something else still has access to the object.

In order to properly close your GUI, you have to invoke a close method. For a JFrame, you can try this:

myGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

which will close the application when the user clicks the close button. If you want to control closing by the program, then you can try:

private void closeItDown() {
  WindowEvent event = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
  Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
}

...and calling this method. You don't have to set anything to null, the JVM will garbage collect for you at this point, maybe. Not guaranteed, but possibly at this point, perhaps later, or now... or whenever :)

Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48
0

you want to make disappear GUI then dispose()

you dont have to worry about this , Garbage collector will automatically take it out of memory or

you can invoke it yourself

System.gc()

but it is bad practice as well as not guaranteed that it will surely being collected.

anshulkatta
  • 2,044
  • 22
  • 30
  • 1
    System.gc() should not be called explicitly - it is considered bad. When you find yourself invoking it yourself, you must know that you are grasping for straws and that you have to find a different solution. – Jaco Van Niekerk May 14 '13 at 05:20
  • by all means please elaborate bad , is it discouraged by oracle or creators of Java, please give me link..?? – anshulkatta May 14 '13 at 05:24
  • Besides, it won't help here because as long as the window is active it will remain reachable ... even if you `null` the reference. – Stephen C May 14 '13 at 05:25
  • 1
    Here's an explanation of why calling `System.gc()` is a bad idea. http://stackoverflow.com/questions/2414105/why-is-it-a-bad-practice-to-call-system-gc/2414621#2414621 – Stephen C May 14 '13 at 05:26
  • 1
    here it is http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/devapps/codeprac.html – anshulkatta May 14 '13 at 05:27
  • (I see the links have been provided...) As a side-note, benchmarking is one area I know of where System.gc() makes sense. Typically System.gc() gets called, followed by a Thread.sleep(...) just before the benchmarking starts. Apparently it gives more accurate results. – Jaco Van Niekerk May 14 '13 at 05:29
  • yes i already looked at that :) – anshulkatta May 14 '13 at 05:30
  • *"Apparently it gives more accurate results."* - It depends on what you mean by more accurate. Yes, it (tends to) remove GC overheads from whatever you are benchmarking. But that means that the performance numbers from your benchmark could be less realistic. It may be a better idea is to leave the GC overheads in, and run sufficient iterations that the GC overheads average out. – Stephen C May 14 '13 at 06:11
0
  1. Deleting an object by setting its reference to null make it eligible for garbage collection and JVM will take care of removal of all those object. If you wants to force the garbage collector to run you can issue System.gc() or Runtime.gc () but it is still not a guarantee that it will be collected back to JVM. You can find further information on how garbage collector works here

  2. If you wants to remove your GUI window you can use dispose() method.

Ashish
  • 14,295
  • 21
  • 82
  • 127
  • Don't call `gc()`. It is a bad idea in general, and in this case it is futile ... unless you have disposed the Window. – Stephen C May 14 '13 at 05:30