I noticed lately by using a reference viewer tool that I've got more and more instances of a JPopupmenu on the heap. Every time I right click I create a new menu and show it. The menu uses actions as menuitems. The problem seems to be, that I use actions that are singletons or used elsewhere. When I add them to the popupmenu they get linked in both directions to the menu item. So, as long an action lives, that long will the popupmenu stay on the heap.
A solution I found is to set the action of all JMenuItems of the popupmenu to null (with setAction(null)) which deregisters the MenuItem as listener and so the Popupmenu is no longer referenced and can be GCed.
The point is, when should this happen? I tried it with popupMenuListener.popupMenuWillBecomeInvisible(). Yeah, it cleans everything fine, but too early. The event is not dispatched to the action anymore.
I think its not a severe problem by now, but now I know that there are useless objects created with every popupmenu and I want to clean them up in the right way.
Did anybody have the same problem? Is there a solution?
- is it a bad idea to use actions that are singletons?
- should I reuse the JPopupmenus (singletons/manager)
- when could I clean up independently? Is there an event I can listen for?
Thank you for reading this post,
hope you've got an answer.