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

Viewed 11829

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?

1 Answers
Related