javascript - Angular:在路线更改时发现内存泄漏
问题描述
我想实现一个功能来检测路由更改时的内存泄漏。
有了这个片段:
const getMemory = () => (window.performance as any).memory.usedJSHeapSize / (window.performance as any).memory.jsHeapSizeLimit;
这个想法是,我检索当前的内存NavigationStart
使用情况并将其与内存使用情况进行比较NavigationEnd
,不同之处在于内存泄漏。
目标是创建一些东西,比如取消订阅。
一个例子:
import { Component } from '@angular/core';
import { Router, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '@angular/router';
@Component({
selector: 'my-app',
template: `
<div class="container">
<a routerLinkActive="active" routerLink="/login">Login</a> |
<a routerLinkActive="active" routerLink="/home">Home</a> |
<a routerLinkActive="active" routerLink="/catalog">Catalog</a>
<router-outlet></router-outlet>
</div>
`,
})
export class AppComponent {
private memoryStart = 0;
constructor(private router: Router){
const getMemory = () => (window.performance as any).memory.usedJSHeapSize / (window.performance as any).memory.jsHeapSizeLimit;
this.router.events.subscribe((event: any) => {
switch (true) {
case event instanceof NavigationStart: {
this.memoryStart = getMemory();
break;
}
case event instanceof NavigationEnd: {
console.log('Memory leak', (getMemory() - this.memoryStart));
break;
}
case event instanceof NavigationCancel:
case event instanceof NavigationError: {
break;
}
default: {
break;
}
}
});
}
}
我的想法正确吗?
解决方案
推荐阅读
- c# - C# 填充列表
使用来自另一个类的字符串和默认构造函数 - c# - AssemblyLoadContext 加载和卸载在不同的函数中得到不同的结果
- selenium - Selenium 测试用例卡在构建代理中。如何解决?
- python - 我正在尝试将一个数字分配给一年中的一个月,它应该使用 if 语句而不是字典
- php - PHP Carbon 在 ->format('H:m') 时增加 1 分钟
- javascript - Visual Studio Code Node.js 创建模块
- .net - 使用 .NET Core 的异步工作者
- javascript - 如何使用分片获取所有用户 ID?
- python - Selenium 和 Instagram:计算传入消息的数量
- javascript - 点击手机屏幕后,导航栏在大屏幕上消失