Why Aren't FormControl#valueChanges' Subscriptions Garbage Collected?

心已入冬 提交于 2020-05-13 07:10:30

问题


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.

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?


回答1:


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.


When component gets destroyed

When manually unsubscribed

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



来源:https://stackoverflow.com/questions/51858832/why-arent-formcontrolvaluechanges-subscriptions-garbage-collected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!