I'd say it depends on myData$ observable.
If it automatically completes, then you should not worry about unsubscribing from it.
But if it doesn't, then here is why I think you should manually unsubscribe.
Suppose you have a shared service, SharedService, which has a private BehaviorSubject and exposes it with the asObservable() method.
class SharedService {
private userSbj = new BehaviorSubject({});
user$ = this.userSbj.asObservable();
}
Let's see what happens when ComponentA subscribes to user$ and consumes it:
class ComponentA {
constructor (private sharedService: SharedService) { }
ngOnInit () {
this.userAge$ = this.sharedService.user$.pipe(pluck('
this.subscription = this.userAge$.subscribe(data => console.log(data));
}
}
userAge$ comes from user$. But remember that user$ is just BehaviorSubject.asObservable(). What this means is that when a new subscriber is registered, it will be added to the subscribers list maintained by the BehaviorSubject.
If you are not manually unsubscribing, then that subscriber will still be part of that , even though the component might be destroyed. That subscribers list still keeps a reference to that subscriber. But, if you do unsubscribe, say in ngOnDestroy:
this.subscription.unsubscribe();
Then, that subscriber will be removed from that subscribers list, so, since the subscriber is no longer referenced from any other place, you will not have any memory leaks.
This is what happens when a BehaviorSubject is being subscribed to:
_subscribe(subscriber: Subscriber<T>): Subscription {
const subscription = super._subscribe(subscriber);
if (subscription && !(<SubscriptionLike>subscription).closed) {
subscriber.next(this._value);
}
return subscription;
}
In super._subscribe(subscriber), super refers to Subject, because BehaviorSubject extends Subject:
Subject._subscribe:
// adding to the subscribers(observers) list
this.observers.push(subscriber);
return new SubjectSubscription(this, subscriber);
SubjectSubscription plays an important role here, since it's responsible for removing the subscriber from the subscribers list.