My Java application allows users to create an interface of controls (GUI) and delete them and place new controls on the interface.
I’m using this code to delete the controls and request a garbage collection (which I know Java will do in its own time).
try {
for (Component c : contentPane.getParent().getComponents()) {
if (!c.toString().contains("JMenuBar") && !c.toString().contains("Panel") ) {
contentPane.getParent().remove(c);
System.out.println("Removed " + c.getClass());
try {
c = null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
System.out.println("Did not remove: " + c.getClass());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.gc();
As far as removing and nulling them out, I believe it works – it throws no errors, but after repeated creating/destroying I got up to 5Gig of memory use and after several days Java still hadn’t cleaned up the memory.
There are threads in constant looping use. Will this prevent Java from gc or is there something else I’m missing to accomplish this?
Thanks in advance.