JavaScript WeakMap继续引用gc’ed对象

我正在体验
JavaScript弱图,在谷歌Chrome开发者控制台中尝试此代码后,使用–js-flags =“ – expose-gc”运行,我不明白为什么弱图继续引用ab如果是gc’ed.

var a = {listener: function(){ console.log('A') }}
a.b = {listener: function(){ console.log('B') }}

var map = new WeakMap()

map.set(a.b, [])
map.set(a, [a.b.listener])

console.log(map) // has both a and a.b

gc()
console.log(map) // still have both a and a.b

a = undefined
gc()
console.log(map) // only have a.b: why does still have a reference to a.b? Should'nt be erased?

最佳答案 在您的示例代码中,您没有发布变量.它是一个顶级var,永远不会超出范围,永远不会被明确地取消引用,所以它保留在WeakMap中.一旦在代码中没有对象的引用,WeakMap / WeakSet就会释放它们.在你的例子中,如果你在一次gc()调用之后调试console.log(a),你仍然期望一个活着,对吗?

所以这是一个工作示例,显示WeakSet的运行情况以及一旦所有引用都消失后它将如何删除:https://embed.plnkr.co/cDqi5lFDEbvmjl5S19Wr/

const wset = new WeakSet();

// top level static var, should show up in `console.log(wset)` after a run
let arr = [1];
wset.add(arr);

function test() {
  let obj = {a:1}; //stack var, should get GCed
  wset.add(obj);
}

test();

//if we wanted to get rid of `arr` in `wset`, we could explicitly de-reference it
//arr = null;

// when run with devtools console open, `wset` always holds onto `obj`
// when devtools are closed and then opened after, `wset` has the `arr` entry,
// but not the `obj` entry, as expected
console.log(wset);

请注意,打开Chrome开发工具可以防止某些对象被垃圾收集,这使得看到这个操作比预期更困难:)

点赞