0

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.

amos
  • 5,092
  • 4
  • 34
  • 43
  • *"I'm trying to track down why my object MyData isn't being garbage collected.."* I recommend not worrying about it unless the app. is causing `OutOfMemoryError` .. – Andrew Thompson Jul 27 '16 at 17:24
  • Good point, but I started looking into this because of an OutOfMemoryError. Each MyData instance can be several 100s of MB on the heap. I reasoned, "oh, I'll just delete one so I can load another" and here I am... I don't want to force users to quit the app because it has become unusable. – amos Jul 27 '16 at 17:44
  • whats reason for `((JPopupMenu) e.getSource()).removeAll();`, because I still to take the JPopup as temporary object – mKorbel Jul 27 '16 at 19:21
  • That's my question -- the popup reference temporary, but apparently there's some kind of "popup manager" behind the scenes hanging on to it. Presumably it would go away eventually... – amos Jul 28 '16 at 12:32

0 Answers0