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 leaking of Observable Maps (), because _hasMap keeps links on all once used keys (key could be object) and never delete them #2031

Closed
BanderasPRO opened this issue Jul 3, 2019 · 10 comments · Fixed by #2032

Comments

@BanderasPRO
Copy link

Issue:
I find that observable Maps absolutely always loose a memory. For every key of the map, you create same key in your private map called _hasMap and place there a boolean value. But these keys are kept there even the origin map is empty or already doesn't contain the key. Don't forget that key could be large object or linked with many other objects. So we loose all memory even we don't keep links (have variables) to these key object in our code, because the _hasMap keeps links on all once used keys (key - is the big object, remember ?) and never delete them.

Please see the example:
https://codesandbox.io/s/minimal-mobx-react-project-1uyb0

Of cause we expected that we don't see the our objects in memory heap, after clearing the map.

Idea:
You should use another mechanism to register of using the map methods and of getting values.

@danielkcz
Copy link
Contributor

I wonder if WeakMap could be helpful here.

@BanderasPRO
Copy link
Author

BanderasPRO commented Jul 3, 2019

@FredyC
It seems WeakMap can't help. _hasMap keep values which are observable boolean, and I think that something was subscribed on that observable to detect an access to a Map, maybe to register dependency on the Map. So, if that's right then the subscription does't allow to free the memory as for key object as for value either.

@BanderasPRO
Copy link
Author

+++
you also loose a memory when you just use 'has' method of a map. Once you used has(key), the key will put into the _hasKey forever

So the observable.map with _hasKey map is deep hole to loose memory

@danielkcz
Copy link
Contributor

Yea, I have no idea why is it implemented like that. Hopefully, @mweststrate can stop by and shed some light onto this.

@mayorovp
Copy link
Contributor

mayorovp commented Jul 4, 2019

We can remove unobserved atoms from this map. We don't need them.

@mweststrate
Copy link
Member

IIRC there is no specific reason for it being implemented this way, than mere legacy. Up to MobX 4 only primitives were supported as map keys, so the problem (barely) existed in that scenario.

A PR that cleans up unobserved entries as suggestd by @mayorovp in the hasMap is welcome!

@mayorovp
Copy link
Contributor

mayorovp commented Jul 4, 2019

@mweststrate what about my PR?

@mweststrate
Copy link
Member

mweststrate commented Jul 4, 2019

Apologies, overlooked that!

@mweststrate
Copy link
Member

Released as 4.13.0 / 5.13.0

@lock
Copy link

lock bot commented Sep 15, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants