Skip to content

Memory leak on adding and removing components #13851

Closed

Description

@nSharifA

Description of the bug

The following snippet results a memory leak:

@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).

Minimal reproducible example

see the code.

Versions

  • Vaadin / Flow version: 14.8.4
  • Java version: 1.8.0_271
  • OS version: Win10
  • Browser version (if applicable): Chrome 101.---
  • Application Server (if applicable): Jetty
  • IDE (if applicable): Eclipse

Activity

  1. nSharifA commented on May 10, 2022

    @nSharifA
    Author

    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.

  2. transferred this issue fromvaadin/flowon May 11, 2022
  3. mcollovati commented on May 11, 2022

    @mcollovati
    Collaborator

    Thanks for reporting and investigation. I've transferred it to the flow-components repo where Button class resides

  4. Artur- commented on May 12, 2022

    @Artur-
    Member

    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.

    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..

  5. Artur- commented on May 12, 2022

    @Artur-
    Member

    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

  6. TatuLund commented on May 13, 2022

    @TatuLund
    Contributor

    @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?

  7. sissbruecker commented on May 24, 2022

    @sissbruecker
    Member

    Moving this back to the flow repo, please check Artur's comment for the root cause: #13851

  8. changed the title [-]Memory leak[/-] [+]Memory leak on adding and removing components[/+] on May 25, 2022
  9. 9 remaining items

  10. vaadin-bot commented on Sep 20, 2022

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with Vaadin 23.1.10.

  11. added 2 commits that reference this issue on Sep 20, 2022
    bb4c638
    a5dbf03
  12. added a commit that references this issue on Sep 22, 2022
    8ba856b
  13. vaadin-bot commented on Sep 22, 2022

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with Vaadin 22.0.23.

  14. added a commit that references this issue on Sep 23, 2022
    18c4058
  15. vaadin-bot commented on Sep 26, 2022

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with Vaadin 14.8.18.

  16. vaadin-bot commented on Sep 26, 2022

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with Vaadin 23.2.2.

  17. vaadin-bot commented on Oct 3, 2022

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with Vaadin 23.3.0.alpha1 and is also targeting the upcoming stable 23.3.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions