You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory leak on adding and removing components #13851
@Push
@Route("test")
@Viewport("width=device-width, initial-scale=1")
public class Test extends Div {
FlexLayout testLayout;
public Test() {
testLayout = new FlexLayout();
add(new Button("Click me", e -> {
testLayout.removeAll();
IntStream.range(0, 10000).forEach(i -> testLayout.add(new Button("B")));
}));
}
}
If you click on the button it will remove the testLayout content and add 10 000 new Buttons to the testLayout.
Expected behavior
After clicking multiple times on the button Java VisualVM should show 10 000 + 1 button instance in the memory after pressing the perform GC button.
Instead it will show (n * 10000) + 1 buttons (where n == click count).
After little debugging i found out the cause. (this problem exists in the newest vaadin 14 version too)
I've noticed disableListener in the Button class and no one calls Registration.remove() on it. So i overridden the Button addClickListener for testing purpose like this:
@Push
@Route("diagram")
@Viewport("width=device-width, initial-scale=1")
public class Test extends Div {
FlexLayout testLayout;
public Test() {
testLayout = new FlexLayout();
add(new Button("Click me", e -> {
testLayout.removeAll();
IntStream.range(0, 10000).forEach(i -> testLayout.add(new Button("B") {
@Override
public Registration addClickListener(ComponentEventListener<ClickEvent<Button>> listener) {
return new Registration() {
@Override
public void remove() {
}
};
}
}));
}));
}
}
So without disableListener on the button class the gc have no problem removing it from memory.
Probably the cause here is in Flow in ElementChildrenList / NodeList. When you add an element to an ElementChildrenList it adds a ListAddChange to be sent to the client. When you remove the element again (when you remove the component) it adds a ListRemoveChange to be sent to the client. These keep accumulating until you actually send the info to the client.
The stay in memory because they are tied to testLayout as it tells about children being added to and removed from that.
You use memory in the same way when excluding the event registration but the absolute number is smaller as the event registration uses memory to store the registration
@Artur- good observation, then I think the fix I proposed is eventually not needed, if the fix is introduced in NodeList instead. Also the problem you mention, might cause small memory leaks with other components too. Also should this ticket be moved back to Flow repo?
Activity
nSharifA commentedon May 10, 2022
After little debugging i found out the cause. (this problem exists in the newest vaadin 14 version too)
I've noticed disableListener in the Button class and no one calls Registration.remove() on it. So i overridden the Button addClickListener for testing purpose like this:
So without disableListener on the button class the gc have no problem removing it from memory.
mcollovati commentedon May 11, 2022
Thanks for reporting and investigation. I've transferred it to the flow-components repo where Button class resides
Artur- commentedon May 12, 2022
Probably the cause here is in Flow in
ElementChildrenList/NodeList. When you add an element to anElementChildrenListit adds aListAddChangeto be sent to the client. When you remove the element again (when you remove the component) it adds aListRemoveChangeto be sent to the client. These keep accumulating until you actually send the info to the client.The stay in memory because they are tied to
testLayoutas it tells about children being added to and removed from that.For this not to happen, it seems like that the comment in https://github.com/vaadin/flow/blob/master/flow-server/src/main/java/com/vaadin/flow/internal/nodefeature/NodeList.java#L280 should be implemented..
Artur- commentedon May 12, 2022
You use memory in the same way when excluding the event registration but the absolute number is smaller as the event registration uses memory to store the registration
TatuLund commentedon May 13, 2022
@Artur- good observation, then I think the fix I proposed is eventually not needed, if the fix is introduced in NodeList instead. Also the problem you mention, might cause small memory leaks with other components too. Also should this ticket be moved back to Flow repo?
sissbruecker commentedon May 24, 2022
Moving this back to the flow repo, please check Artur's comment for the root cause: #13851
[-]Memory leak[/-][+]Memory leak on adding and removing components[/+]9 remaining items
vaadin-bot commentedon Sep 20, 2022
This ticket/PR has been released with Vaadin 23.1.10.
fix: Be more sensible when adding changes to list of pending changes (#…
fix: Be more sensible when adding changes to list of pending changes (#…
fix: Be more sensible when adding changes to list of pending changes (#…
vaadin-bot commentedon Sep 22, 2022
This ticket/PR has been released with Vaadin 22.0.23.
fix: Be more sensible when adding changes to list of pending changes (#…
vaadin-bot commentedon Sep 26, 2022
This ticket/PR has been released with Vaadin 14.8.18.
vaadin-bot commentedon Sep 26, 2022
This ticket/PR has been released with Vaadin 23.2.2.
vaadin-bot commentedon Oct 3, 2022
This ticket/PR has been released with Vaadin 23.3.0.alpha1 and is also targeting the upcoming stable 23.3.0 version.