简体   繁体   中英

GWT Garbage Collection on unused Widgets?

I am interested in the question when and if the GWT garbage collection will be triggered in the following example.

I have a Composite:

class A extends Composite implements HasClickHandlers {

  ...

  FocusPanel panel = new FocusPanel();

    @Override
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        return focusPanel.addClickHandler(handler);
    } 

}

In a view class I create this widget add a click handler and add this widget to a panel:

A widget = new A();
widget.addClickHandler(someClickHandler);
somePanel.add(widget);

At some point in the application I clear the panel where I have entered the widget. So the widget is not attached to the DOM anymore and there is no reference pointing on this widget.

void removeAll() {
   somePanel.clear();
}

What happens with the widget and the click handler. Does the GWT garbage collection takes care of it? Do I have to save the handler registration and remove the click handler myself?

One of the advantages to the widget system is that it manages all of these memory issues for you. Provided that you only combined widgets by adding them to other widgets (and never use getElement().appendChild(...), etc), all handlers will be automatically wired to the dom when they are attached, and unwired when either they are detached or the page is unloaded.

This means that you just treat the widgets like normal objects - if they aren't attached to the page anymore and you don't hold a reference to them, they will be correctly garbage collected no matter the browser.

The particular memory leaks you are referring to are only a problem in older browsers - modern Webkit/Gecko (Chrome/Safari/Opera and Firefox) do not have these problems, nor do IE 10 or 11.

Check out http://www.gwtproject.org/articles/dom_events_memory_leaks_and_you.html for a deeper discussion of how Widgets implement this memory leak prevention code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM