Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content(rxjs): avoid memory leaks #37

Merged
merged 2 commits into from
Jun 12, 2019
Merged

Conversation

thekiba
Copy link
Contributor

@thekiba thekiba commented Jan 19, 2019

Use publishReplay and refCount instead of shareReplay to avoid memory leaks.
See ReactiveX/rxjs#3336

Use `publishReplay` and `refCount` instead of `shareReplay` to avoid memory leaks.
See ReactiveX/rxjs#3336
@KwintenP
Copy link
Member

Hi,

It actually depends on the use case that you have. If you have an http Observable that will just give you one value, than you can perfectly use this.

If you'd want to change this to something that reflects some nuance on when to use which one, I'm definitely up for it. You can read about it in this article that I wrote: https://blog.strongbrew.io/share-replay-issue/

@thekiba
Copy link
Contributor Author

thekiba commented Jan 23, 2019

@KwintenP
It doesn't matter because:
uses the Observable interface, hoping for its completion makes the code fragile;
subscription cannot be canceled, it is likely will be critical side effects.

The application should work stably, and not fall with every unexpected change.

@d3lm
Copy link
Member

d3lm commented Jan 28, 2019

There's an open PR that adds an option to shareReplay but it is still open and not merged yet. I think I would agree that in most cases you'd want to unsubscribe from the source when the ref count goes from 1 to 0. But as Kwinten said, that really depends on the use case and to be on the safe side it's a good idea to use publishReplay and refCount. As long as there is no option to enable this behavior we should advocate the safer option here.

@d3lm
Copy link
Member

d3lm commented Jan 30, 2019

The PR was merged. shareReplay now accepts a config object. More specifically, the option refCount can be used to control the behavior. If you set the following config { refCount: true } then the subscription to the source is reference counted. By default it's not. We should definitely mention this.

@KwintenP
Copy link
Member

KwintenP commented Feb 4, 2019

I'll update this asap with the refCount and a link to the publishReplay(1).refCount()

@d3lm
Copy link
Member

d3lm commented Jun 7, 2019

@thekiba Would you like to change this PR to the latest API of shareReplay to avoid possible memory leaks with shareReplay(1). Now that this PR was merged, I think we can change it.

@thekiba
Copy link
Contributor Author

thekiba commented Jun 7, 2019

@d3lm Thanks for, I can.

But what do you think about that? ReactiveX/rxjs#4530
shareReplay still has different behavior with publishReplay and refCount.

@d3lm
Copy link
Member

d3lm commented Jun 11, 2019

Thanks for changing your PR. I think that what's discussed in the issue you mentioned is for this checklist item negligible because shareReplay, with the new config option, doesn't cause memory leaks anymore. The point of discussion in that issue is that it still doesn't behave the same way as publishReplay().refCount() but that is by design, to make shareReplay and other variants such as share retryable as opposed to the solution with publishReplay.

I think we can merge this one.

@KwintenP What do you say?

@KwintenP
Copy link
Member

SGTM. Thanks for your addition!

@KwintenP KwintenP merged commit e928777 into typebytes:master Jun 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants