0

I've just learned about memory leak of the RxJS Object. So I created a small code to test if it ok when I unsubscribe the Subject. Here is my code:

let subj = new rxjs.Subject();
const sub1 = subj.subscribe(text => console.log(text));
subj.next('subject 1');
subj.unsubscribe();
subj = new rxjs.Subject();
const sub2 = subj.subscribe(text => console.log(text));
subj.next('subject 2');
subj.unsubscribe();

In Chrome's devtool, I find out that the Rx objects can be seen in the Heap snapshot. Please explain to me why, and is it ok in term of memory leak?enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

gianglaodai
  • 301
  • 1
  • 10
  • I think you should also assign `subj`, `sub1` and `sub2` to `null`. – martin Dec 29 '19 at 10:34
  • @martin I updated the code. So your suggestion is correct. So Is it mean we should always assign RxJS objects to null after call unsubscribe on it? Most of the RxJS pages I read so far never mention it. – gianglaodai Dec 29 '19 at 10:40
  • I'm pretty sure that's ok. If you can still see it there it's because you've declared it as a global variable but if you do that inside a function once the function as been called it should be garbage collected. The only important thing is to unsubscribe – maxime1992 Dec 29 '19 at 11:41
  • @maxime1992 Great. I'll happy to accept your answer – gianglaodai Dec 29 '19 at 12:04
  • Does this answer your question? [Angular RxJS Observable: takeUntil vs. unsubscribe with a Subscription](https://stackoverflow.com/questions/58428791/angular-rxjs-observable-takeuntil-vs-unsubscribe-with-a-subscription) – wentjun Dec 29 '19 at 12:13
  • @wentjun Nope. As you see in my code, I unsubscribe all the subject but they are still in the Heap memory – gianglaodai Dec 29 '19 at 12:19
  • AFAICT, you are calling of `unsubscribe` on the subjects and that is almost never what you want: https://ncjamieson.com/closed-subjects/ – cartant Dec 29 '19 at 12:52
  • @cartant This post means there are different behavior when I call unsubscribe on the subjects and the subscribers. So I tried to call unsubscribe on the subscribers after the call to unsubscribe the subjects. Then I still see the subjects and the subscribes in the memory – gianglaodai Dec 29 '19 at 14:00

1 Answers1

0

Alternatively, you can unsubcribe using the takeWhile() RxJS operator, and according to the documentation, it

Emit values until provided expression is false.

You may use it through the use of pipeable operators,

subj
  .pipe(
    takeUntil(this.unsubscribe$),
  ).subscribe(text => console.log(text));

The above subscription will be unsubscribed when unsubscribe$ subject is 'completed'.

this.unsubscribe$.next();
this.unsubscribe$.complete();
wentjun
  • 40,384
  • 10
  • 95
  • 107