components icon indicating copy to clipboard operation
components copied to clipboard

[Sidenav] Memory leak when used inside router-outlet

Open npen opened this issue 6 years ago • 10 comments

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

npen avatar Dec 07 '17 12:12 npen

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

https://github.com/npen/material2/commit/fe21cd78a321ae90753a72a22f726956c9defff3

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

npen avatar Dec 20 '17 12:12 npen

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

Abrissirba avatar Feb 09 '18 09:02 Abrissirba

@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 :laughing:

mmalerba avatar Feb 09 '18 16:02 mmalerba

PR #13378 should fix the leak issue described above.

sheiidan avatar Nov 27 '18 21:11 sheiidan

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.

FionaTreveil avatar Jan 25 '19 16:01 FionaTreveil

On doing some further investigation, I think that the problem that I am seeing relates to bug https://github.com/angular/angular/issues/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 ?

FionaTreveil avatar Feb 07 '19 10:02 FionaTreveil

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

mmalerba avatar Feb 07 '19 17:02 mmalerba

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 https://github.com/angular/angular/issues/18606 is also marked as fixed, so I will open a new issue.

FionaTreveil avatar Feb 18 '19 11:02 FionaTreveil

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 https://github.com/angular/angular/issues/24197 to demonstrate the general work-around is effective with the sidenav component too.

jsim5446 avatar Jun 07 '20 19:06 jsim5446

any news on this? still leaking memory for sidenav

kevinclerc avatar Jul 27 '22 09:07 kevinclerc

This is still reproducible :(

toxik avatar Apr 18 '23 14:04 toxik

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

blasco avatar Jul 12 '23 08:07 blasco