0

Does component.getChildren().clear() remove event listeners

asked 2019-07-01 16:01:38 +0800

JustinFrost gravatar image JustinFrost
141 1 6

updated 2019-07-01 16:02:28 +0800

I am creating a bunch of components dynamically in java - add manually creating event listeners for them as follows:

Idspace linkWrapper = new Idspace();
linkWrapper.setSclass("link-wrapper");

Button linkButton = new Button();
linkButton.addEventListener(Events.ON_CLICK, new org.zkoss.zk.ui.event.EventListener() {
    @Override
    public void onEvent(Event event) throws Exception {
    ...
    }
});
linkWrapper.appendChild(linkButton);

Do I have to remove all of the event listeners for these buttons manually or will

linkWapper.getChildren().clear()

remove all of the event listeners for me ?

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2019-07-02 10:21:08 +0800

cor3000 gravatar image cor3000
4596 2 7
ZK Team

updated 2019-07-02 10:33:12 +0800

yes if you don't keep references to those components/event listeners in your application code they'll be garage collected my the JVM.

You can observe this directly by taking a heap dump before and after a mass-component detachment. (e.g. using visual-vm). Then check the instance count of EventListener objects.

Did you observe something different indicating a memory leak?

link publish delete flag offensive edit
0

answered 2019-07-02 10:28:40 +0800

cor3000 gravatar image cor3000
4596 2 7
ZK Team

If you are using the same event listener implementation for multiple link buttons, consider implementing a shared event listener, to even reduce the memory foot print while the event listeners are attached.

// create a single listener
EventListener listener = (MouseEvent event) => {
  Button link = event.getTarget(); 
  /*do something component specific*/ 
};

/*some loop*/
...
  linkButton.addEventListener(Events.ON_CLICK, listener); /*use the same instance*/
  linkWrapper.appendChild(linkButton);
...
/*end loop*/

Now the event listener will be garbage collected when the last component using it is detached/garbage collected.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2019-07-01 16:01:38 +0800

Seen: 6 times

Last updated: Jul 02

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More