I'm trying to track down why my object MyData isn't being garbage collected, and one problem I'm seeing is that the JPopupMenu, which contains several Actions containing references to the MyData instance, hasn't been collected (verified by VisualVM).
How/when should those be cleared?
I've got this:
private void mousePressEventFired(MouseEvent e) {
JPopupMenu popup = new JPopupMenu();
controller.customizePopupForObject(popup,null,null,
selectedNode.getUserObject());
popup.show(e.getComponent(),e.getX(),e.getY());
}
where I've trimmed from this sample the work I've done to call customizePopupForObject
. The customizePopupForObject
contains numerous lines that look like this:
popup.add(new MyDataAction1(myData,this,false));
I'm using Java 1.7.0_79 on OSX. I started down the path of setting all references from within the popup items to be WeakReferences, but I'm not sure that's how it's supposed to work.
I tried adding this:
popup.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(
PopupMenuEvent e) {
}
@Override
public void popupMenuWillBecomeInvisible(
PopupMenuEvent e) {
((JPopupMenu) e.getSource()).removeAll();
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
});
but that doesn't seem like the right approach.
Slightly relatedly... even when I'm not trying to delete my data, the popup behaves a bit weird to me. If I trigger it in places where the items are different, I can see it flicker visually -- first image is what the popup was, and then it gets updated to what its supposed to be.