Dialog causes a memory leak when Dialog component contains mat-form-field
What is the expected behavior?
Instantiating a dialog with a component that contains mat-form-field element causes a DOM node memory leak.
What is the current behavior?
Memory leak
What are the steps to reproduce?
https://stackblitz.com/edit/angular-yx1kuw?file=app%2Fdialog-overview-example-dialog.html
Try to open and close the dialog several times. Notice each time the number of detached DOM nodes gets higher and forcing garbage collection doesn't reduce it.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular Material: 7.3.7 Chrome: 73.0.3683.103
Is there anything else we should know?
Could be related to https://github.com/angular/material2/issues/10188
This is caused by @angular/animations package failing to clean up cached elements. The issue occurs when both a parent component (in this case dialog-container) and a child component (mat-form-field-hint-wrapper) has a set of animation triggers. A couple of issues tracking leaks are potentially related:
https://github.com/angular/angular/issues/30728 https://github.com/angular/angular/issues/30693
Contrary to other error cases, every time Dialog component is opened, a new set of elements is cached and subsequently leaked. This is due to a new namespaceId being created each time a dialog is opened. Thus the leaks build up rather than taper off in this case.
The layout is roughly like so:
<dialog container> <-- has triggers
<mat-form-field>
<mat-form-field-hint-wrapper> <-- has triggers
An issue should probably be raised for @angular/animations package
@rhartvig The memory leak still persists even after removing the animations module
@njofce , I guess you're using NoopAnimationsModule instead of BrowserAnimationsModule? If so, I guess that doesn't change the offending code paths. IIRC the issue is in packages/animations/browser/src/render/transition_animation_engine.ts (https://github.com/angular/angular/blob/master/packages/animations/browser/src/render/transition_animation_engine.ts) where the cache structures would not get cleared for all the keys used.
@rhartvig Yes, you are right.
Hi, does anyone have a workaround for this? We have a lot of forms inside dialogs in our app and seeing a huge build up of unreleased nodes.
@jpduckwo Try to use ngModel instead of FormGroups/FormControl
@jpduckwo , I can offer a workaround that address the leaks I am seeing:
https://stackblitz.com/edit/angular-yx1kuw-fgdzjg
Notice in dialog component constructor:
this.isClosing$ = this.dialogRef.beforeClosed().pipe(mapTo(true));
and surrounding dialog content:
<ng-container *ngIf="!(isClosing$ | async)">
...
Ie. a manual destroy of dialog content before dialog is closed.
To see how this makes a difference:
- From devtools press ctrl+p (or Cmd+p) to open the file transition_animation_engine.ts
- Set a breakpoint in
clearElementCachearound the line:
const elementPlayers = this._engine.playersByElement.get(element);
- Open and close dialog multiple times and notice how
this._engine.playersByElementdoes not continue to build up elements, contrary to original stackblitz.
Thanks so much guys, that's a great idea I'll give it a test in our app. I'm thinking of having a stab at creating a pull request to fix this issue, bit crazy when you think about it really...
I believe this could be the root cause issue: angular/angular/issues/25744
Should've been fixed in 9.0.0-rc.7 by https://github.com/angular/angular/commit/835ed0f35fa187f4051129ab30b7b635f15aeb57.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.