New Information added:
I tried out a lot of stuff to get rid of a JDialog (see full information below) with the help of a helpful topic. I reworked code from last answer in this topic to add a propertyChangeListener, which seems to be in my real code the problem:
running, reworked code 1:
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class RemoveDialogOnruntimeListener extends JFrame {
private static final long serialVersionUID = 1L;
private boolean runProcess;
private int maxLoop = 0;
private Timer timer;
private JOptionPane optionpane;
private JDialog dialog;
public RemoveDialogOnruntimeListener() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setTitle("Remove Dialog On Runtime");
setLocation(150, 150);
pack();
setVisible(true);
addNewDialog();
}
private void addNewDialog() {
this.optionpane = new JOptionPane();
this.dialog = optionpane.createDialog("Foo");
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModalityType(Dialog.ModalityType.MODELESS);
dialog.setVisible(true);
this.optionpane.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
// Object obj = evt.getNewValue();
// System.out.println("----");
// System.out.println(obj);
RemoveDialogOnruntimeListener.this.optionpane = null;
RemoveDialogOnruntimeListener.this.dialog = null;
remWins();
}
});
}
private void remWins() {
runProcess = true;
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (runProcess) {
for (Window win: Window.getWindows()) {
if (win instanceof JDialog) {
System.out.println(" Trying to Remove JDialog");
win.dispose();
}
}
System.out.println(" Remove Cycle Done :-)");
runProcess = false;
new Thread() {
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Runtime.getRuntime().gc();
}
}.start();
} else {
if(maxLoop>=2){
timer.stop();
} else {
pastRemWins();
runProcess = true;
}
}
}
});
timer.setRepeats(true);
timer.start();
}
private void pastRemWins() {
maxLoop++;
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");
} else if (wins[i] instanceof JDialog) {
System.out.println("JDialog");
} else {
System.out.println(wins[i].getClass().getSimpleName());
}
}
// We must expect 2 windows here: this (RemoveDialogOnRuntime) and the parent of all parentless dialogs
if (wins.length > 2) {
wins = null;
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();
}
} else {
timer.stop();
}
}
private void closeMe() {
System.exit(0);
}
public static void main(String args[]) {
RemoveDialogOnruntimeListener superConstructor = new RemoveDialogOnruntimeListener();
}
}
In this example, with usage of the listener, still the JDialog will be deleted. I tried to work in relevant code to my actual class as follows:
public class DeleteInsideBox3DDialog {
Visu3DDeleteInsideBox visu;
JFormattedTextField xCoord,yCoord,zCoord;
SimpleTree simpleTree;
List<Point3D> lastDeleted;
Object[] options;
JDialog dialog;
JOptionPane optionPane;
private boolean runProcess;
private int maxLoop = 0;
private Timer timer;
public DeleteInsideBox3DDialog(SimpleTree simpleTree, Visu3DDeleteInsideBox visu) {
this.simpleTree = simpleTree;
this.visu = visu;
this.lastDeleted = new ArrayList<Point3D>();
this.options = new String[] { "Done" };
Object complexMsg[] = { centerXPanel(),centerYPanel(),centerZPanel()
,dimXPanel(),dimYPanel(),dimZPanel()
,deleteButtonPanel(),undeleteButtonPanel() };
this.optionPane = new JOptionPane();
this.optionPane.setMessage(complexMsg);
this.optionPane.setOptions(this.options);
this.optionPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
// this.dialog = this.optionPane.createDialog(this.simpleTree.getGui()
// .getMonitor().getSelectedComponent(),
// "Delete Points contained in a box");
this.dialog = this.optionPane.createDialog("Delete Points contained in a box");
this.dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.dialog.setModalityType(Dialog.ModalityType.MODELESS);
// this.dialog.setSize(800, 400);
this.dialog.setVisible(true);
this.dialog.setVisible(false);
this.dialog.setVisible(true);
this.dialog.pack();
this.optionPane.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Object obj = evt.getNewValue();
int result = -1;
for (int k = 0; k < DeleteInsideBox3DDialog.this.options.length; k++) {
if (DeleteInsideBox3DDialog.this.options[k].equals(obj)) {
result = k;
}
}
if (result == 0) {
DeleteInsideBox3DDialog.this.optionPane = null;
DeleteInsideBox3DDialog.this.dialog.setVisible(false);
DeleteInsideBox3DDialog.this.dialog.dispose();
DeleteInsideBox3DDialog.this.dialog = null;
remWins();
// Visu3DDeleteInsideBox visuOld = (Visu3DDeleteInsideBox) DeleteInsideBox3DDialog.this.simpleTree.getGui().getMonitor().getSelectedComponent();
// visuOld.setDialog(null);
// DeleteInsideBox3DDialog.this.visu = null;
// Monitor monitor = DeleteInsideBox3DDialog.this.simpleTree.getGui().getMonitor();
// monitor.removeAll();
// monitor.revalidate();
// new Thread() {
// @Override
// public void run() {
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// Runtime.getRuntime().gc();
// }
// }.start();
//Visu3dBasic visu = new Visu3dBasic(DeleteInsideBox3DDialog.this.simpleTree, monitor.getWidth(), monitor.getHeight());
//monitor.add(visu,DeleteInsideBox3DDialog.this.simpleTree.getModel().getTreeID());
//DeleteInsideBox3DDialog.this.optionPane.removeAll();
// DeleteInsideBox3DDialog.this.optionPane = null;
//DeleteInsideBox3DDialog.this.dialog.setVisible(false);
//DeleteInsideBox3DDialog.this.dialog.dispose();
// DeleteInsideBox3DDialog.this.dialog = null;
// DeleteInsideBox3DDialog.this.remWins();
}
}
});
// this.optionPane = null;
// this.dialog.setVisible(false);
// this.dialog.dispose();
// this.dialog = null;
// remWins();
}
private void remWins() {
runProcess = true;
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (runProcess) {
for (Window win: Window.getWindows()) {
if (win instanceof JDialog) {
System.out.println(" Trying to Remove JDialog");
try {
win.getParent().remove(win);
} catch (Exception e1) {
}
((JDialog) win).setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
((Dialog) win).setModalityType(Dialog.ModalityType.MODELESS);
win.dispose();
}
}
System.out.println(" Remove Cycle Done :-)");
runProcess = false;
new Thread() {
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Runtime.getRuntime().gc();
}
}.start();
} else {
if(maxLoop>=2){
timer.stop();
} else {
pastRemWins();
runProcess = true;
}
}
}
});
//timer.setRepeats(true);
timer.start();
}
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");
} else if (wins[i] instanceof JDialog) {
System.out.println("JDialog");
} else {
System.out.println(wins[i].getClass().getSimpleName());
}
}
// We must expect 2 windows here: this (RemoveDialogOnRuntime) and the parent of all parentless dialogs
if (wins.length > 2) {
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 ***");
//System.exit(0);
timer.stop();
}
} else {
timer.stop();
}
}
}
This is not running, what is actual a running class (where the JDialog is deleted) is the same class with the 5 relevant lines contained in the Listener put to the end with also removing the listener.
public DeleteInsideBox3DDialog(SimpleTree simpleTree, Visu3DDeleteInsideBox visu) {
this.simpleTree = simpleTree;
this.visu = visu;
this.lastDeleted = new ArrayList<Point3D>();
this.options = new String[] { "Done" };
Object complexMsg[] = { centerXPanel(),centerYPanel(),centerZPanel()
,dimXPanel(),dimYPanel(),dimZPanel()
,deleteButtonPanel(),undeleteButtonPanel() };
this.optionPane = new JOptionPane();
this.optionPane.setMessage(complexMsg);
this.optionPane.setOptions(this.options);
this.optionPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
// this.dialog = this.optionPane.createDialog(this.simpleTree.getGui()
// .getMonitor().getSelectedComponent(),
// "Delete Points contained in a box");
this.dialog = this.optionPane.createDialog("Delete Points contained in a box");
this.dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.dialog.setModalityType(Dialog.ModalityType.MODELESS);
// this.dialog.setSize(800, 400);
this.dialog.setVisible(true);
this.dialog.setVisible(false);
this.dialog.setVisible(true);
this.dialog.pack();
// this.optionPane.addPropertyChangeListener(new PropertyChangeListener() {
//
// @Override
// public void propertyChange(PropertyChangeEvent evt) {
// Object obj = evt.getNewValue();
// int result = -1;
// for (int k = 0; k < DeleteInsideBox3DDialog.this.options.length; k++) {
// if (DeleteInsideBox3DDialog.this.options[k].equals(obj)) {
// result = k;
// }
// }
//
// if (result == 0) {
//// DeleteInsideBox3DDialog.this.optionPane = null;
//// DeleteInsideBox3DDialog.this.dialog.setVisible(false);
//// DeleteInsideBox3DDialog.this.dialog.dispose();
//// DeleteInsideBox3DDialog.this.dialog = null;
//// remWins();
//// Visu3DDeleteInsideBox visuOld = (Visu3DDeleteInsideBox) DeleteInsideBox3DDialog.this.simpleTree.getGui().getMonitor().getSelectedComponent();
//// visuOld.setDialog(null);
//// DeleteInsideBox3DDialog.this.visu = null;
//// Monitor monitor = DeleteInsideBox3DDialog.this.simpleTree.getGui().getMonitor();
//// monitor.removeAll();
//// monitor.revalidate();
//// new Thread() {
//// @Override
//// public void run() {
//// try {
//// Thread.sleep(100);
//// } catch (InterruptedException e) {
//// e.printStackTrace();
//// }
//// Runtime.getRuntime().gc();
//// }
//// }.start();
// //Visu3dBasic visu = new Visu3dBasic(DeleteInsideBox3DDialog.this.simpleTree, monitor.getWidth(), monitor.getHeight());
// //monitor.add(visu,DeleteInsideBox3DDialog.this.simpleTree.getModel().getTreeID());
// //DeleteInsideBox3DDialog.this.optionPane.removeAll();
//// DeleteInsideBox3DDialog.this.optionPane = null;
// //DeleteInsideBox3DDialog.this.dialog.setVisible(false);
// //DeleteInsideBox3DDialog.this.dialog.dispose();
//// DeleteInsideBox3DDialog.this.dialog = null;
//// DeleteInsideBox3DDialog.this.remWins();
// }
//
// }
// });
this.optionPane = null;
this.dialog.setVisible(false);
this.dialog.dispose();
this.dialog = null;
remWins();
}
The only difference in the two real code fragments seems to be the usage of the Listener changing everything from working to not working, but when I tried to produce example code, also the code with the Listener is working.
Before edit:
I have an application written for processing terrestrial laser-scanning data (between 1 mio and 20 mio 3d points). I cleaning up my code right now and reworking different classes. Now I get memory usage issues which I dont know where they come from.
Basic Information:
OS : Win 7 Ultimate 64 Bit SP1
Ram : 16GB
Project execution environment : JavaSE-1.7
As the project is quiet complex, I guess it will be very hard to provide a running example. Basically the steps, after I started my application with following parameters are:
-XX:-UseGCOverheadLimit
-Xms14g
-Xmx14g
Load a point cloud in asci format and store them in a Point class (class name Point3D) implementing Point3f (sorry for confusing names) from vecmath lib and visualize it. Perform one of the following operations, which are all invoking a JDialog in my Gui:
spatial clustering or
deleting points inside a deletion box
Whenever I redo one of the operations, the memory usage shown in the task manager is enlarged. Lets stick to the deletion of points. I press a button in my gui invoking the following class: