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

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

问题描述

我经历了很多话题,说一个人需要从FormControl#valueChanges退订,以防止内存泄漏.我了解了Observable退订的何时"和如何".据我了解,产生无限个值的Observable需要取消订阅,而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天全站免登陆