Reputation: 1776
I have an Angular form array of groups. Elsewhere in the app some subscriptions are setup on formcontrol validation changes. At some point in the life cycle I delete a formgroup. Do I need to be concerned that subscriptions are still open on objects that arent in memory anymore?
Upvotes: 4
Views: 2369
Reputation: 15196
NO
If you call .subscribe anywhere in your code, the only way to not leak memory is to make sure those subscriptions gets unsubscribed again.
You need to either manually unsubscribe each one or make sure the observables you subscribe to are all finite - meaning they have an "end" signal somewhere. They can be either a limited sequence (.first(), .take(), etc) or they can be toggled off by ending in a .takeWhile() or .takeUntil()
The subscriptions does not care if you leave scope, remove referenced object or even navigate away. You need to make sure they get unsubscribed.
See my answer here: RXJS - Angular - unsubscribe from Subjects
Upvotes: 2
Reputation: 3253
Do I need to be concerned that subscriptions are still open on objects that arent in memory anymore?
As long as the subscription/the observable is open and running the variable will still be in memory even if your parent element is not available anymore. Thus leading to leaking memory.
Golden rule: You should always unsubscribe observables.
There are certain exceptions/edge cases to this rule, for example Http Requests automatically unsubscribe themselves after completing, thus theoretically not needing to unsubscribe.
edit: I found an article that explains it more in depth Angular/RxJs When should I unsubscribe from `Subscription`
Upvotes: 1