es6 中 Map 和 WeakMap 集合的垃圾收集
Garbage collection on Map and WeakMap collections in es6
我正在阅读 WeakMap 的描述,它说:
In native WeakMaps, references to key objects are held "weakly", which means that they do not prevent garbage collection in case there would be no other reference to the object.
通过阅读本文,我的理解是 WeakMaps 用于您试图利用 JavaScript 的垃圾收集的实例。我根本不理解的是我加粗的那一行。
为什么在普通 Map 中,没有引用的对象不会被垃圾回收?这有什么好处,我是否必须从普通地图中明确删除?
在正常的Map
中,Map
是对象的引用
"Why, in a normal Map, would objects with no references to them not be garbage collected?" 因为地图本身有对象的引用。只要有引用,对象就不会被gced。 - 菲利克斯·克林
我正在阅读 WeakMap 的描述,它说:
In native WeakMaps, references to key objects are held "weakly", which means that they do not prevent garbage collection in case there would be no other reference to the object.
通过阅读本文,我的理解是 WeakMaps 用于您试图利用 JavaScript 的垃圾收集的实例。我根本不理解的是我加粗的那一行。
为什么在普通 Map 中,没有引用的对象不会被垃圾回收?这有什么好处,我是否必须从普通地图中明确删除?
在正常的Map
中,Map
是对象的引用
"Why, in a normal Map, would objects with no references to them not be garbage collected?" 因为地图本身有对象的引用。只要有引用,对象就不会被gced。 - 菲利克斯·克林