为什么不收集 FormControl#valueChanges 的订阅垃圾? [英] Why Aren't FormControl#valueChanges' Subscriptions Garbage Collected?

查看:25
本文介绍了为什么不收集 FormControl#valueChanges 的订阅垃圾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过很多线程说需要取消订阅 FormControl#valueChanges 以防止内存泄漏.我了解取消订阅 Observable 的何时"和如何".据我了解,需要取消订阅产生 无限 个值的 Observables,而 FormControl#valueChanges 就是这样一种 Observable.

I have gone through plenty of threads saying that one needs to unsubscribe from FormControl#valueChanges to prevent memory leaks. I understood the "when" and "how" to unsubscribe from Observables. As I understand, Observables that produce infinite number of values need to be unsubscribed and FormControl#valueChanges is one such Observable.

但我的问题是,为什么不收集这些 Observables 的垃圾?我的意思是当一个 Angular 组件被销毁时,引用就死了,对吗?模板中的表单控件不再存在.FormControl 实例也不见了.因此,如果 Observable 的源",即 FormControl 消失了,那么它的成员 valueChanges 怎么还存在并保留其订阅?

But my question is, why aren't these Observabless garbage collected? I mean when an Angular component gets destroyed, the references are dead right? The form control in the template is no longer there. The FormControl instance is also gone. So if the "source" of the Observable, the FormControl, is gone then how come its member valueChanges still exist and retain its subscriptions?

推荐答案

好的,所以我使用 Chrome Dev Tools 中的 Memory Profiler 进行了一些实验.我发现,无论您取消订阅 FormControl#valueChanges 还是组件被破坏,结果几乎相同.在这两种情况下,一个 SubjectSubscription 都会被垃圾收集.看看下面的结果.

Okay so I performed some experiments with the Memory Profiler in Chrome Dev Tools. What I found was, whether you unsubscribe from FormControl#valueChanges or the component gets destroyed the result is almost the same. In both cases one SubjectSubscription gets garbage collected. Take a look at the results below.

所以我认为可以肯定地说订阅确实被删除了,不需要手动取消订阅.

So I think it's safe to say that the subscriptions do in fact get removed and one does not need to unsubscribe manually.

这篇关于为什么不收集 FormControl#valueChanges 的订阅垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆