Skip to content

[Sidenav] Memory leak when used inside router-outlet #8862

Open

Description

@npen

Bug, feature request, or proposal:

When a sidenav is used "inside" router-outlet, it seems there is a memory leak. When the router-outlet is inside the sidenav, there is no problem.

I use the "Take heap snapshot" of Chrome to see the problem.

What are the steps to reproduce?

Sample available on:

https://stackblitz.com/edit/angular-material2-issue-xmgukp?file=app%2Fapp.component.ts

Open in Chrome:

https://angular-material2-issue-xmgukp.stackblitz.io/

  • Switch between "one" and "two" routes a few times.
  • Take a heap snapshot
  • Switch again between routes
  • Take another snapshot and compare => OneComponent and TwoComponent are not freed (among many other objects...)

Activity

  1. changed the title [-]Memory leak when sidenav is inside router-outlet[/-] [+][Sidenav] Memory leak when used inside router-outlet[/+] on Dec 12, 2017
  2. npen commented on Dec 20, 2017

    @npen
    Author

    If it can help I found a few problems in drawer.ts:

    npen@fe21cd7

    As you can see in this commit, there is still one line which seems ok to me, but that needs to be commented out to completely fix the leak in the sample...

  3. added
    P3
    perf
    on Jan 9, 2018
  4. Abrissirba commented on Feb 9, 2018

    @Abrissirba

    I did some digging on this and found that if I set dir="ltr" on the mat-sidenav-container it doesn't leek anymore. I wasnt't able to figure out why though...

  5. mmalerba commented on Feb 9, 2018

    @mmalerba
    Contributor

    @npen yeah looks like it should work to me, maybe try subscribing in ngAfterContentInit instead of the constructor? or if dir="ltr" somehow makes it work perhaps add a startWith(null)? I'm just kinda guessing at things though 😆

  6. sheiidan commented on Nov 27, 2018

    @sheiidan

    PR #13378 should fix the leak issue described above.

  7. FionaTreveil commented on Jan 25, 2019

    @FionaTreveil

    I am not an expert in either javascript or Angular, but I currently have a similar problem to this.

    I have component with mat-sidnav-container, mat-sidenav and mat-sidenav-content which is used within a child route. It is therefore being used with a router-outlet inside a parent router-outlet.

    I am using version 7.2.2 of @angular/material, but after each display of this component, all the nodes inside the mat-sidenav-container are left hanging and are never free to be garbage collected.

    The same code frees up all the nodes if I use

    instead of mat-sidenav.

    Thank you for any help you can provide.

  8. FionaTreveil commented on Feb 7, 2019

    @FionaTreveil

    On doing some further investigation, I think that the problem that I am seeing relates to bug angular/angular#18606. The mat-drawer code is including an animation trigger with HostBinding.
    When the drawer is destroyed, the animation information is not released, leaving references in playersByElement/statesByElement in TransitionAnimationEngine. The result is that all of the nodes within the drawer are left as detached elements when the sidenav is removed.

    Is there a way that I can manually free up this animation ?

  9. mmalerba commented on Feb 7, 2019

    @mmalerba
    Contributor

    It looks like that issue has been fixed, if you're still able to reproduce it you may want to comment there or open a new issue on angular/angular

  10. FionaTreveil commented on Feb 18, 2019

    @FionaTreveil

    This problem still exists, but is not caused directly by the sidenav component, but as a result of the HostBinding on the animation code. It looks like the bug at angular/angular#18606 is also marked as fixed, so I will open a new issue.

  11. jsim5446 commented on Jun 7, 2020

    @jsim5446

    Here is a repro that demonstrates this issue is still not fixed in Angular 9, but also shows a work-around that can eliminate the memory leak:

    https://stackblitz.com/edit/angular-ivy-u1fvgw

    As @FionaTreveil mentioned in previous comment, this is actually a bug in the core Angular animations package, not limited to the Angular Material sidenav component. The stackblitz example included here is just an adaptation of the one I referenced in my comment in angular/angular#24197 to demonstrate the general work-around is effective with the sidenav component too.

  12. kevinclerc commented on Jul 27, 2022

    @kevinclerc

    any news on this? still leaking memory for sidenav

  13. toxik commented on Apr 18, 2023

    @toxik

    This is still reproducible :(

  14. blasco commented on Jul 12, 2023

    @blasco

    Hi, I'm also experiencing this issue. Any updates?

  15. ChristianKlima commented on Jan 12, 2024

    @ChristianKlima

    Hello, we are also experiencing this issue. Our components cannot be properly cleaned up when we add to the template. There doesn't need to be any content in the sidenav. If we remove this tag, memory cleanup works fine. Are there any updates on this?

  16. ciaranj commented on Jul 3, 2024

    @ciaranj

    Thank you @jsim5446, four years later, this is still an issue in angular 18, and I can confirm that your hint of adding: animations: [trigger("noop", [])] to the parent component does indeed resolve the memory leak.

  17. Emil-Blomquist commented on Sep 4, 2024

    @Emil-Blomquist

    @ciaranj are you sure that the workaround completely solves the issue? For me, prior, there is a leak every time I navigate back and forth. That is, after N visits the leak has occurred N times. However, after adding animations: [trigger("noop", [])] there is only a single leak, independent of N.

  18. ciaranj commented on Sep 4, 2024

    @ciaranj

    Hmm, I don’t have that project to hand currently. However, I may have just assumed that that single ‘leak’ was just something lazily initialised by angular, so didn’t treat it as a leak as it wasn’t leading to any memory growth?

  19. Emil-Blomquist commented on Sep 5, 2024

    @Emil-Blomquist

    Yea you might right, perhaps I'm being a little pedantic!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: material/sidenavperf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions