angular / components

Component infrastructure and Material Design components for Angular
https://material.angular.io
MIT License
24.22k stars 6.69k forks source link

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

Open npen opened 6 years ago

npen commented 6 years ago

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/

npen commented 6 years ago

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

Abrissirba commented 6 years ago

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

mmalerba commented 6 years ago

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

sheiidan commented 5 years ago

PR #13378 should fix the leak issue described above.

FionaTreveil commented 5 years ago

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 commented 5 years ago

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 ?

mmalerba commented 5 years ago

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

FionaTreveil commented 5 years ago

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.

jsim5446 commented 4 years ago

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.

kevinclerc commented 1 year ago

any news on this? still leaking memory for sidenav

toxik commented 1 year ago

This is still reproducible :(

blasco commented 12 months ago

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

ChristianKlima commented 5 months ago

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?

ciaranj commented 4 days ago

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.