angular / angular/components

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

未关闭
#30,775 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
area: material/form-field needs investigation P4
主要语言
TypeScript
星标
25k
派生
6.8k
平均合并
1 天 8 小时
30 天内合并 PR
91

描述

### 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](https://stackblitz.com/github/EverydayWebDev/simple-material-project/tree/mat-input)
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](https://github.com/user-attachments/assets/18d6746b-d617-4369-8b4f-17b684e6909b)

Actual Behavior:
Dev tools memory profiling shows 1 Detached Node

8 -> 12 -> 16 ... detached
2 -> 3 -> 4 ... detached

贡献指南

打开贡献指南

调研方向

先从链接的三个复现项目开始,分别对应 mat-form-field/input、mat-tab 和 mat-menu,然后复现路由切换并检查 Chrome 堆快照。比较所述 Angular Material 版本之间不断累积的分离节点。完成的标准是,相关的分离 DOM 节点会被垃圾回收,并且在重复导航后不会累积。

由索引模型根据 Issue 内容生成。

评估

技术栈
angular, typescript
领域
frontend, performance
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。