内存中未释放的订户

时间:2019-07-04 07:16:39

标签: angular memory-management rxjs

我目前正在将Angular v5与rxjs v.5.5.6结合使用。

我想减少我的angular 5组件中的内存泄漏。我已经实施了"official"推荐的策略,方法是在每次订阅之前先将takeUntil(this.destroy $)放入管道。

   import {takeUntil} from 'rxjs/operators';
   import { Subject } from 'rxjs/Subject';
   ...
   private destroy$ = new Subject();
   ... 

   // sample subscriber
   this.adminService.GetCompanySettings('potatoCorp').pipe(
    takeUntil(this.destroy$)
   ).subscribe(res => {}, null, null);

   // sample mergemap combinator
   this.basicMemberService.currentMember.pipe(
     mergeMap(currentMember => { 
     // do some internal logic
     return this.utilityService.getMemberInfo(currentMember.Id)/* 'Tried this'.pipe(
    takeUntil(this.destroy$))
    */
    }),
     takeUntil(this.destroy)
    ).subscribe(result => {
      // do something here
    }
)


   ngOnDestroy() {
   this.destroy$.next();
   this.destroy$.complete();

我尝试将pipe(takeUntil)策略添加到mergeMap中返回的observable中,但效果不明显。

A screenshot of two heap snapshots. The first one is made from just staying at the homepage. The second is made after going into memberDetails, then returning to the homepage.

第一个是仅停留在首页上。第二步是在进入memberDetails之后,然后返回到主页。

为什么在内存中保留了这么多(各种类型)的订户?目前,每当用户执行最基本的操作时,堆就会增加约5 MB。

0 个答案:

没有答案