Search code examples
rxjsobservablesubscribe

RxJS .subscribe() without arguments still cause memory leak?


I know this will cause memory leak because there is no unsubscription

interval(100).subscribe(console.log);

question is: If I do not pass any observer into subscribe method, is it still causing memory leak? thanks

interval(100).subscribe();

Solution

  • Yes, it's the same.

    You can check it inside source code of Observable:

    ...
      subscribe(
        observerOrNext?: Partial<Observer<T>> | ((value: T) => void) | null,
        error?: ((error: any) => void) | null,
        complete?: (() => void) | null
      ): Subscription {
        const subscriber = isSubscriber(observerOrNext) ? observerOrNext : new SafeSubscriber(observerOrNext, error, complete);
    ...
    

    There is SafeSubscriber class instance creation method if no arguments passed. And subscription works normally as you'd expect.