Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to complete a Subject for it to be garbage collected?

Tags:

I follow a cleanup pattern in my Angular components that looks like this:

class SomeComponent implements OnInit, OnDestroy {     private destroy$ = new Subject();      ngOnInit() {         service.someStream().takeUntil(this.destroy$).subscribe(doSomething);     }      ngOnDestroy() {         this.destroy$.next(true);     } } 

This has the benefit of automatically unsubscribing when the component is destroyed.

My question is: Does a reference to destroy$ stick around indefinitely because I haven't called this.destroy$.complete(), or will it get GC'ed when the parent class is collected?

like image 826
Sean Walsh Avatar asked May 31 '17 16:05

Sean Walsh


People also ask

Does observable complete unsubscribe?

In these cases, the observable will call . complete() after it has emitted all of it's values. There's no need to unsubscribe. It completes on it's own, which means it unsubscribes all subscribers automatically.

What is subject in RxJS?

A subject in RxJS is a special hybrid that can act as both an observable and an observer at the same time. This way, data can be pushed into a subject, and the subject's subscribers will, in turn, receive that pushed data.


1 Answers

If you look at the source for Subject.complete, you'll find the answer:

complete() {   if (this.closed) {     throw new ObjectUnsubscribedError();   }   this.isStopped = true;   const { observers } = this;   const len = observers.length;   const copy = observers.slice();   for (let i = 0; i < len; i++) {     copy[i].complete();   }   this.observers.length = 0; } 

Calling complete notifies any observers and then clears the array of observers. Unless you have an observer/subscriber that has a reference to the Subject, there is nothing in the complete implementation that would affect whether or not the Subject could be garbage collected.

RxJS pushes notifications to subscribers. Subscribers don't hold references to the observables; it's the other way around. So, unless you've explicitly created a subscriber that holds a reference to the Subject - via a closure or some other mechanism - there's no need to call complete for garbage-collection purposes.

like image 129
cartant Avatar answered Sep 23 '22 06:09

cartant