Subscribe on changes!

WeakMap or WeakSet give a false sense of memory safety

avatar
Jul 26th 2021

Version

3.2.0-beta.5

Reproduction link

link

Steps to reproduce

  1. const a = reactive(myWeakMap)
  2. a.set(myObj, 1)
  3. const v = a.get(myObj) // Enforce tracking on myObj key
  4. deference myObj
  5. 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: memleak

avatar
Jul 26th 2021

Closing, as this is not a Weak-only issue, but also counts for other reactive Maps or Sets when keys are set to object references.