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

Memory leak caused due to create too many instances of Api object #11

Open
xlc opened this issue Apr 8, 2019 · 4 comments
Open

Memory leak caused due to create too many instances of Api object #11

xlc opened this issue Apr 8, 2019 · 4 comments

Comments

@xlc
Copy link

xlc commented Apr 8, 2019

Currently every instance of the Api object will create and maintain a web socket connection.
It never disconnects itself and will attempt to reconnect if connection dropped for any reason.

We have an issue that developers are not aware of this behavior and construct a new instance of Api on every method call.
Because the Api object can never automatically GC'ed by JS due to the fact the ws connection is open, this results connection leaks, memory leaks, and unstable application.

I don't know what is the best way to solve this issue other than code review.
I wonder if have polkadot.js enforce shared Api object will help. i.e. have a shared pool of all created api instances, return existing one if parameters (ws url) matches.

@xlc xlc changed the title Enforce singleton? Or shared ws connection? Memory leak caused due to create too many instances of Api object Apr 8, 2019
@amaury1093
Copy link

Yes, I also saw this on 1-2 occasions.

I kind of disagree for a shared singleton pool though, I think we shouldn't fix developers' mistakes with ugly patches (ugly = i don't like singletons). And imo two new Api(...) should always create 2 WS subscriptions.

So I would propose:

  • Add this to the docs!
  • We could add a singleton pool, but only to show a console.warn() on new new Api(...)

@jacogr
Copy link
Member

jacogr commented Apr 10, 2019

Marking as docs, not quite open to singleton polls without as specific (switched off by default), flag for this.

@jacogr
Copy link
Member

jacogr commented Feb 19, 2020

Ok, not sure wtf to add to the documentation here. Where is the current getting started lacking that can be adjusted? (Asking since my first instinct was just to close is)

@xlc
Copy link
Author

xlc commented Feb 19, 2020

I guess in this page there can be a section about teardown an instance and mention if you don't teardown it, the instance will live in memory and may not be GC'ed and therefore could cause memory leak.

Or another page about all the thing that developers should be aware of and this will be one of section.

@jacogr jacogr transferred this issue from polkadot-js/api Oct 6, 2020
@jacogr jacogr added the @api label Oct 6, 2020
@jacogr jacogr added the -size-m label May 28, 2021
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

No branches or pull requests

3 participants