JPanel with anonymous EventListener - why doesn't GC destroy listener?

后端 未结 2 1501
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 02:33

I have been perusing the open source code of JMapViewer. If anyone else wishes to look at it, check the SVN.

In a nutshell, the main class is JMapViewer

相关标签:
2条回答
  • 2020-12-12 02:54

    1) Everything you know is that, if and when the VM deems it to be appropriate, it will collect some or all of the dead objects. The GC is not required to do anything.

    2) The best thing is to ask to the maintainer of the library. Anyway, as a general rule, I would not bother to change anything unless there's a good reason to, e.g. if it sensibly improves readability, and would rather focus myself on real problems.

    3) Not sure if that's the case, but, when you serialize a JComponent, you also serialize all of its fields. And you do not want to serialize a lot of unused stuff.

    0 讨论(0)
  • 2020-12-12 02:59

    The abstract parent, JMapController, holds a reference to the JMapViewer passed there by the DefaultMapController constructor:

    public DefaultMapController(JMapViewer map) {
        super(map);
    }
    

    Addendum: The map reference held by the controller is used to (selectively) add up to three controller references to the map's EventListenerList, discussed here. Any one of these would preclude GC. At least one salutary design benefit is that a concrete JMapController need only implement available interfaces.

    As suggested in this MVC outline, it would be unusual to give the view a reference to the controller. In contrast, there's nothing wrong with letting the controller register as a listener to the view, as suggested here.

    Note that only the no-argument JMapViewer constructor installs a DefaultMapController. You can use the alternate constructor, as noted in comments at line 57-59 in revision 29113 of Demo.java. A complete example is examined here.

    0 讨论(0)
提交回复
热议问题