简体   繁体   English

是否在未使用的小部件上使用GWT垃圾回收?

[英]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. 我对以下示例中何时以及是否触发GWT垃圾回收的问题感兴趣。

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. 因此,该小部件不再附加到DOM,并且此小部件上没有指向的引用。

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

What happens with the widget and the click handler. 小部件和点击处理程序会发生什么。 Does the GWT garbage collection takes care of it? GWT垃圾回收会处理吗? 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. 假设您仅通过将小部件添加到其他小部件中来组合小部件(并且从不使用getElement()。appendChild(...)等),则所有处理程序在连接时都将自动连接到dom,并在它们被连接时自动取消连接分离或页面被卸载。

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. 您所指的特定内存泄漏仅是较旧的浏览器中的问题-现代Webkit / Gecko(Chrome / Safari / Opera和Firefox)没有这些问题,IE 10或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. 请访问http://www.gwtproject.org/articles/dom_events_memory_leaks_and_you.html ,以更深入地讨论Widget如何实现此内存泄漏预防代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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