不幸的是,似乎这个最近关闭的问题没有得到很好的理解。这是典型的输出:
run: Trying to Remove JDialog Remove Cycle Done :-) Checking if still exists any of TopLayoutContainers JFrame JDialog Will Try Remove Dialog again, CycleNo. 1 ----------------------------------------------------------- Trying to Remove JDialog Remove Cycle Done :-) Checking if still exists any of TopLayoutContainers JFrame JDialog Will Try Remove Dialog again, CycleNo. 2 ----------------------------------------------------------- Trying to Remove JDialog Remove Cycle Done :-) Checking if still exists any of TopLayoutContainers JFrame JDialog Will Try Remove Dialog again, CycleNo. 3 ----------------------------------------------------------- Trying to Remove JDialog Remove Cycle Done :-) Checking if still exists any of TopLayoutContainers JFrame JDialog *** End of Cycle Without Success, Exit App *** BUILD SUCCESSFUL (total time: 13 seconds)
我再试一次问这个问题:我如何能K 我 L * L于运行第一开顶层Container,并与关闭对我摇摆恶梦帮助?
import java.awt.*; import java.awt.event.WindowEvent; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; public class RemoveDialogOnRuntime extends JFrame { private static final long serialVersionUID = 1L; private int contID = 1; private boolean runProcess; private int top = 20; private int left = 20; private int maxLoop = 0; public RemoveDialogOnRuntime() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setPreferredSize(new Dimension(300, 300)); setTitle("Remove Dialog On Runtime"); setLocation(150, 150); pack(); setVisible(true); Point loc = this.getLocation(); top += loc.x; left += loc.y; AddNewDialog(); } private void AddNewDialog() { DialogRemove firstDialog = new DialogRemove(); remWins(); } private void remWins() { runProcess = true; Thread th = new Thread(new RemTask()); th.setDaemon(false); th.setPriority(Thread.MIN_PRIORITY); th.start(); } private class RemTask implements Runnable { @Override public void run() { while (runProcess) { Window[] wins = Window.getWindows(); for (int i = 0; i < wins.length; i++) { if (wins[i] instanceof JDialog) { System.out.println(" Trying to Remove JDialog"); wins[i].setVisible(false); wins[i].dispose(); WindowEvent windowClosing = new WindowEvent(wins[i], WindowEvent.WINDOW_CLOSING); wins[i].dispatchEvent(windowClosing); Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing); Runtime runtime = Runtime.getRuntime(); runtime.gc(); runtime.runFinalization(); } try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(RemoveDialogOnRuntime.class.getName()).log(Level.SEVERE, null, ex); } } wins = null; SwingUtilities.invokeLater(new Runnable() { @Override public void run() { System.out.println(" Remove Cycle Done :-)"); Runtime.getRuntime().runFinalization(); Runtime.getRuntime().gc(); runProcess = false; } }); } pastRemWins(); } } private void pastRemWins() { System.out.println(" Checking if still exists any of TopLayoutContainers"); Window[] wins = Window.getWindows(); for (int i = 0; i < wins.length; i++) { if (wins[i] instanceof JFrame) { System.out.println("JFrame"); wins[i].setVisible(true); } else if (wins[i] instanceof JDialog) { System.out.println("JDialog"); wins[i].setVisible(true); } } if (wins.length > 1) { wins = null; maxLoop++; if (maxLoop <= 3) { System.out.println(" Will Try Remove Dialog again, CycleNo. " + maxLoop); System.out.println(" -----------------------------------------------------------"); remWins(); } else { System.out.println(" -----------------------------------------------------------"); System.out.println("*** End of Cycle Without Success, Exit App ***"); closeMe(); } } } private void closeMe() { EventQueue.invokeLater(new Runnable() { @Override public void run() { System.exit(0); } }); } private class DialogRemove extends JDialog { private static final long serialVersionUID = 1L; DialogRemove(final Frame parent) { super(parent, "SecondDialog " + (contID++)); setLocation(top, left); top += 20; left += 20; setPreferredSize(new Dimension(200, 200)); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setModalityType(Dialog.ModalityType.MODELESS); pack(); setVisible(true); } private DialogRemove() { setTitle("SecondDialog " + (contID++)); setLocation(top, left); top += 20; left += 20; setPreferredSize(new Dimension(200, 200)); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setModalityType(Dialog.ModalityType.MODELESS); pack(); setVisible(true); } } public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { @Override public void run() { RemoveDialogOnRuntime superConstructor = new RemoveDialogOnRuntime(); } }); } }
调用dispose()允许主机平台回收重量级对等方消耗的内存,但只有在事件在上处理后才能这样做。即使这样,也是一个建议。WINDOW_CLOSINGEventQueuegc()
dispose()
WINDOW_CLOSINGEventQueuegc()
附录:查看噩梦的另一种方法是通过探查器。使用运行下面的示例jvisualvm,你可以看到定期收集永远不会完全返回基线。我从人为的小堆开始夸大了垂直轴。其他示例在此处显示。当内存非常有限时,我使用了两种方法:
紧急:从命令行循环,每次启动一个新的VM。
紧急:完全消除重量级组件,无头运行,BufferedImage仅使用2D图形和轻量级组件组成。
BufferedImage
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(); } } }); } }