Remove Top-Level Container on Runtime
Invoking dispose()
allows the host platform to reclaim memory consumed by the heavyweight peer, but it can't do so until after the WINDOW_CLOSING
event is processed on the EventQueue
. Even then, gc()
is a suggestion.
Addendum: Another way to see the nightmare is via a profiler. Running the example below with jvisualvm
, one can see that periodic collection never quite returns to baseline. I've exaggerated the vertical axis by starting with an artificially small heap. Additional examples are shown here. When memory is very limited, I've used two approaches:
Emergent: Loop from the command line, starting a new VM each time.
Urgent: Eliminate the heavyweight component entirely, running headless and composing in a
BufferedImage
using 2D graphics and lightweight components only.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
/** @see https://stackoverflow.com/questions/6309407 */
public class DialogClose extends JDialog {
public DialogClose(int i) {
this.setTitle("Dialog " + String.valueOf(i));
this.setPreferredSize(new Dimension(320, 200));
}
private void display() {
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
passSomeTime();
this.setVisible(false);
this.dispatchEvent(new WindowEvent(
this, WindowEvent.WINDOW_CLOSING));
this.dispose();
passSomeTime();
}
private void passSomeTime() {
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace(System.err);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
int count = 0;
while (true) {
new DialogClose(count++).display();
}
}
});
}
}
destroy Jframe object
Is there a way in your user1.txt file to notice if that user is an administrator or not?
Your question isn't very clear, but you should be able to do something like this:
JFrame frame = new JFrame();
frame.dispose();
The compiler will literally dispose of this frame and automatically clean up using the garbage collector.
How to close a previous Frame when a new Frame is opened
Can somebody please tell me how can i do so so that once a new frame
is about to open the old frame closes..
use
CardLayout
, then there no reason to playing withTop-Level Container
s, put there required number ofJPanels
, then just to switch betweens JPanel viewsas
@trashgod
notified, really not good idea, wayotherwise have to clean-up (remove all contents) uselless container, because Top-Level Containers never will be GC'ed
Is it safe to dispose a JDialog in a try block, and then continue executing code in a matching finally block?
No, as far as you access only non-graphical objects such as string from your example.
Are native screen resources automatically released when the JVM exits?
Everything owned by a process is released when the process exits, unless you are talking about pathological operating systems like Netware 3 & 4.
How to get rid of dead windows in Java?
I found the following code inside the project:
private void disposeWindow(final java.awt.Window window) {
SwingEnvironment.runOnEventDispatchingThread(new Runnable() {
@Override
public void run() {
window.dispose();
window.setVisible(false);
window.removeNotify();
String oldName = window.getName();
if (oldName == null) {
oldName = "unnamed";
}
if (!oldName.startsWith("dead-")) {
window.setName("dead-" + oldName);
}
}
});
}
So this is the reason the windows where named 'dead-'.
However they were properly disposed and still showed up in the Windows Array that is returned by the java.awt.Window.getWindows()
. To get rid of them, I had to create a separate ThreadGroup
and create a separate AppContext
via SunToolkit.createNewAppContext();
. Disposing that AppContext also disposed the Windows properly in my case. See also http://kingsfleet.blogspot.de/2009/10/how-to-have-more-than-one-instance-of.html.
How to detect if one swing window is above another one
Simply use Window.isFocusableWindow(), to check as to which JFrame
lies on top of all others.
Though I will also insist on not using more than a single JFrame
, for any application :-)
Related Topics
Do Line Endings Differ Between Windows and Linux
Relation Between Memory Host and Memory Arguments Xms and Xmx from Java
Distinguishing Between Java Threads and Os Threads
Java Refuses to Start - Could Not Reserve Enough Space For Object Heap
Why Maven Uses Jdk 1.6 But My Java -Version Is 1.7
Eclipse/Tomcat: Deploy Doesn't Work Any More (Classnotfoundexception)
Rjava Load Error in Rstudio/R After "Upgrading" to Osx Yosemite
Selenium: How to Stop Geckodriver Process Impacting Pc Memory, Without Calling Driver.Quit()
Is Java "Pass-By-Reference" or "Pass-By-Value"
The Use of Multiple Jframes: Good or Bad Practice
Should I Avoid the Use of Set(Preferred|Maximum|Minimum)Size Methods in Java Swing
How to Use the Tostring Method in Java
How to Get the Current Working Directory in Java
Floating Point Arithmetic Not Producing Exact Results
Efficiency of Java "Double Brace Initialization"
Inconsistent Performance Applying Foregroundactions in a Jeditorpane When Reading Html
Why Don't Java'S +=, -=, *=, /= Compound Assignment Operators Require Casting