Search code examples
garbage-collectionvaadinvaadin23

Vaadin 23 correctly remove and destroy component


Right now in order to remove the ComboBox element from the FormLayout I use the following method:

formLayout.getElement().removeChild(locationComboBox.getElement());

I still have a validation binder associated with locationComboBox. I may remove binding in the following way:

binder.removeBinding(locationComboBox);

that's works fine. But I'm thinking about the locationComboBox ? Is it enough to just remove the component via removeChild method, or I should call some additional logic in order to completely destroy the locationComboBox element and remove it from the memory? Or this is a task for JVM GC?


Solution

  • formLayout.getElement().removeChild(locationComboBox.getElement()) is equivalent to formLayout.remove(locationComboBox) minus some check that the element is actually a child of the parent.

    Both will effectively remove the combo box from the layout, but there may some other hard references to the component (from the binder, or from some listeners) that prevents it from being garbage collected.