bug(input/mat-form-field): Detached DOM nodes are not garbage collected and results in memory leak when switching between lazy loaded routes #30775
Labels
needs triage
This issue needs to be triaged by the team
Is this a regression?
The previous version in which this bug was not present was
Description
Background:
I've been using angular material for a couple of years together with a team and recently our application has grown so that we have around 1400 inputs on one screen for example. We 've come across on problems that our application started to becoming slow after switching between different screens/routes after a while. It turned out to be memory leaks (Detached nodes that are not garbage collected) and some of the leaks we 've managed to fix because of missing unsubscribtion in our application but others we can't fix and seems to be input and mat-form-field related. We've implemented a workaround that we reload page when memory hits defined limit so we can still deliver a product however angular material related memory leaks are beyond our control.
Description:
First of all I've created a simple example project that has two screens each in separate module screen-one.module.ts and screen-two.module.ts and those modules are lazy loaded and has its own routes 'screen-one' and 'screen-two'. Screen one is just empty screen so it doesn't have memory leaks and is used as reference and screen two contains some material components.
Steps to reproduce:
Example 1: StackBlitz Material input example
Screen two contains only mat-form-field with nested input.
Actual Behavior:
Dev tools memory profiling shows 1 Detached Node <div class="mat-form-field-flex" DOM that wasn't garbage collected. Fortunately If I repeat reproduction steps again and again the number of detached <div class="mat-form-field-flex" remains the same.
Expected Behavior:
All input and mat-form-field related detached DOM nodes are not present in memory.
Example 2: StackBlitz Material input with mat-tab example
Screen two contains only mat-form-field with input that is wrapped with mat-tab and mat-tab-group
Actual Behavior:
Dev tools memory profiling shows 4 Detached Nodes <div class="mat-form-field-flex" DOM that wasn't garbage collected. If I repeat reproduction steps again and again the number of detached <div class="mat-form-field-flex" is accumulating so I have 4 -> 8 -> 12 -> 16 ... detached <div class="mat-form-field-flex" DOM nodes
I 've repeated reproduction steps around 20 times and reached approximately 100 Detached Nodes <div class="mat-form-field-flex"
Expected Behavior:
All input and mat-form-field related detached DOM nodes are not present in memory.
Example 3: StackBlitz Material input with mat-menu example
Screen two contains only mat-form-field with input and in app component I am using mat-menu instead.
Actual Behavior:
Dev tools memory profiling shows 1 Detached Node <div class="mat-form-field-flex" DOM that wasn't garbage collected. If I repeat reproduction steps again and again the number of detached <div class="mat-form-field-flex" is accumulating so I have 1 -> 2 -> 3 -> 4 ... detached <div class="mat-form-field-flex" DOM nodes
Expected Behavior:
All input and mat-form-field related detached DOM nodes are not present in memory.
Important remarks:
I can observe the same behavior outside of stackblitz on my local environment and here you can find example project source code:
Example 1
Example 2
Example 3
I've checked it on angular material v19 and I see the same behavior
Example 1
Example 2
Example 3
Reproduction
Please refer to description above:
Example 1: StackBlitz Material input example
Example 2: StackBlitz Material input with mat-tab example
Example 3: StackBlitz Material input with mat-menu example
Expected Behavior
As in description
Actual Behavior
As in description
Environment
Affected version:
Angular material: v14.2.7 and even in latest angular material v19
Angular: v14.2.13 and even in latest angular v19
NodeJS: v18.20.8
Browser: Chrome Version 134.0.6998.178 (Official Build) (64-bit) Windows 10
The text was updated successfully, but these errors were encountered: