Skip to content

bug(input/mat-form-field): Detached DOM nodes are not garbage collected and results in memory leak when switching between lazy loaded routes #30775

Open
@EverydayWebDev

Description

@EverydayWebDev

Is this a regression?

  • Yes, this behavior used to work in the previous version

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.

  1. Start with Screen One
  2. Navigate to screen two by clicking Screen Two button on left menu
  3. Navigate back to Screen One by clicking on menu button
  4. Open Chrome Dev tools navigate to the memory tab (in case of stackblitz select VM instance webcontainer) and take a heap snapshot

Image

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.

Image

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

  1. Start with Screen One
  2. Navigate to screen two by clicking Screen Two button on left menu
  3. Navigate back to Screen One by clicking on menu button
  4. Open Chrome Dev tools navigate to the memory tab (in case of stackblitz select VM instance webcontainer) and take a heap snapshot

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

Image

I 've repeated reproduction steps around 20 times and reached approximately 100 Detached Nodes <div class="mat-form-field-flex"

Image

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.

  1. Start with Screen One
  2. Navigate to Screen Two by clicking Screen Two button on left menu
  3. Navigate back to Screen One by clicking on menu button
  4. Open Chrome Dev tools navigate to the memory tab (in case of stackblitz select VM instance webcontainer) and take a heap snapshot

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

Image

Expected Behavior:
All input and mat-form-field related detached DOM nodes are not present in memory.

Important remarks:

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

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4A relatively minor issue that is not relevant to core functionsarea: material/form-fieldneeds investigationA member of the team needs to do further investigation to determine the root cause

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions