In my java application, when I close an opened model, some objects are not getting garbage collected. I am using Eclipse MAT to detect the paths to GC root and making sure that the references are set to null after model close so that GC can clean up the memory.
I am not able to clear the following references (an excerpt from Eclipse MAT output).
myTree javax.swing.JTree
uiTreeExpansionListener, keyListener, focusListener javax.swing.plaf.basic.BasicTreeUI$Handler
pressedPath javax.swing.tree.TreePath
lastPathComponent myClass
If the myTree (JTree) does not reset/clear the value in the listeners, then a reference is maintained in lastPathComponent to an object of myClass and it is not garbage collected.
Any idea on how to tell the tree to not maintain references?
Edit: I have put the following method to clear the references, but it does not clear all (the above mentioned) the references.
public void clear() {
cancelEditing();
collapseAll();
clearSelection();
clearToggledPaths();
resetKeyboardActions();
fireTreeCollapsed(null);
}
I found a workaround (not a clean and elegant solution) to at least take care of my problem. The following code does the job of releasing all the (known) references.
public void clear() { cancelEditing(); clearSelection(); clearToggledPaths(); resetKeyboardActions(); updateUI(); }Since I need to execute the above method when I close the model, the performance may not suffer much. But I would prefer a more elegant (and conceptually correct) solution if possible.