WeakMap or WeakSet give a false sense of memory safety
Version
3.2.0-beta.5
Reproduction link
Steps to reproduce
- const a = reactive(myWeakMap)
- a.set(myObj, 1)
- const v = a.get(myObj) // Enforce tracking on myObj key
- deference myObj
- myObj will not be GCed because it is in the depsMap of 'a'
What is expected?
As the user is using a WeakMap, he could expect that, when all references to myObj are gone, myObj can be GCed
What is actually happening?
myObj can never be GCed as, due to tracking, it was added to the depsMap (which is a Map rather than a WeakMap)
It is impossible to convert depsMap to a WeakMap because it needs to be iterated when clearing. In fact, I don't think that it's easily possible to support weak collections in the reactivity module.
It might be better if users are forced to just use a normal Map rather than a WeakMap (or Set rather than a WeakSet) by disallowing proxying a WeakMap/WeakSet. Or warn that the user should actually be using a Map/Set and to make clear that keys should be deleted manually.
After clicking, the object is not removed and retained by the depsMap: