Mobx: Memory leaking of Observable Maps (), because _hasMap keeps links on all once used keys (key could be object) and never delete them

Created on 3 Jul 2019  路  10Comments  路  Source: mobxjs/mobx

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.

馃悰 bug 馃憮 needs investigation

All 10 comments

I wonder if WeakMap could be helpful here.

@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.

+++
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

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

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

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!

@mweststrate what about my PR?

Apologies, overlooked that!

Released as 4.13.0 / 5.13.0

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kmalakoff picture kmalakoff  路  4Comments

joey-lucky picture joey-lucky  路  3Comments

etinif picture etinif  路  3Comments

cafreeman picture cafreeman  路  4Comments

Niryo picture Niryo  路  3Comments