简体   繁体   English

es6中Map和WeakMap集合上的垃圾收集

[英]Garbage collection on Map and WeakMap collections in es6

I was reading the WeakMap's description and it said: 我正在阅读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中,对关键对象的引用保持“弱”,这意味着它们不会阻止垃圾收集,以防没有对该对象的其他引用

From reading this, my understanding is that WeakMaps are used in instances where you're trying to take advantage of JavaScript's garbage collection. 通过阅读本文,我的理解是WeakMaps用于您试图利用JavaScript的垃圾收集的实例。 What I don't understand fundamentally is the line I bolded. 从根本上我不明白的是我加粗的那条线。

Why, in a normal Map, would objects with no references to them not be garbage collected? 为什么在普通的Map中,没有引用它们的对象不会被垃圾收集? What's the advantage to this, and do I have to explicitly delete from normal Maps? 这有什么好处,我是否必须从普通地图中明确删除?

"Why, in a normal Map, would objects with no references to them not be garbage collected?" “为什么在普通的Map中,没有引用它们的对象不会被垃圾收集?” because the map itself has a reference to the object. 因为地图本身具有对象的引用。 As long as there is a reference, the object is not gced. 只要有引用,该对象就不会被引入。 - Felix Kling - 菲利克斯克林

在法线MapMap 对象的引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM