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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With