最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ecmascript 6 - JavaScript(ES6) WeakMap garbage collection when set an object to null - Stack Overflow

programmeradmin9浏览0评论

I've just read that WeakMaps take advantage of garbage collection by working exclusively with objects as keys, and that assigning an object to null is equivalent to delete it:

let planet1 = {name: 'Coruscant', city: 'Galactic City'};
let planet2 = {name: 'Tatooine', city: 'Mos Eisley'};
let planet3 = {name: 'Kashyyyk', city: 'Rwookrrorro'};

const lore = new WeakMap();
lore.set(planet1, true);
lore.set(planet2, true);
lore.set(planet3, true);
console.log(lore); // output: WeakMap {{…} => true, {…} => true, {…} => true}

Then I set the object equal to null:

planet1 = null;
console.log(lore); // output: WeakMap {{…} => true, {…} => true, {…} => true}

Why is the output the same? Wasn't it supposed to be deleted so that the gc could reuse the memory previously occupied later in the app? I would appreciate any clarification. Thanks!

I've just read that WeakMaps take advantage of garbage collection by working exclusively with objects as keys, and that assigning an object to null is equivalent to delete it:

let planet1 = {name: 'Coruscant', city: 'Galactic City'};
let planet2 = {name: 'Tatooine', city: 'Mos Eisley'};
let planet3 = {name: 'Kashyyyk', city: 'Rwookrrorro'};

const lore = new WeakMap();
lore.set(planet1, true);
lore.set(planet2, true);
lore.set(planet3, true);
console.log(lore); // output: WeakMap {{…} => true, {…} => true, {…} => true}

Then I set the object equal to null:

planet1 = null;
console.log(lore); // output: WeakMap {{…} => true, {…} => true, {…} => true}

Why is the output the same? Wasn't it supposed to be deleted so that the gc could reuse the memory previously occupied later in the app? I would appreciate any clarification. Thanks!

Share Improve this question asked Apr 15, 2018 at 10:51 Bruno MazzaBruno Mazza 7151 gold badge11 silver badges24 bronze badges 3
  • 1 "assigning an object to null is equivalent to delete it" - no, removing the reference from your variable does make the object eligible for garbage collection (when that happens). It does not immediately delete anything. – Bergi Commented Apr 15, 2018 at 12:55
  • stackoverflow.com/questions/38203446/… – Bergi Commented Apr 15, 2018 at 12:58
  • Also, these things will all get garbage collected as soon as GC runs since WeakMap doesn't retain a reference to the object. You need to store references to the planets somewhere else or the WeakMap will immediately throw them away. – podperson Commented Feb 25, 2021 at 22:43
Add a comment  | 

2 Answers 2

Reset to default 17

Garbage collection does not run immediately. If you want your example to work you need to force your browser to run garbage collection.

Run chrome with the following flag: google-chrome --js-flags="--expose-gc".

You can now force the garbage collection by calling the global gc() method.

I was trying to figure out the same thing, in 2022, but it looks like the browsers don't bother to garbage collect if the WeakMap is small.

But if you do something like this:

let weakmap = new WeakMap()

for (let i = 0; i < 2000; i++) {
    let john = {meow: i}

    weakmap.set(john, i)
    console.log(weakmap)
    john = null

    let div = document.createElement("div")
    div.innerText = i
    document.body.append(div)
    weakmap.set(div, i)
    div.remove()
}

let john2 = {} // Test to see if it garbage collects this (it shouldn't)

weakmap.set(john2, "random")

console.log(weakmap)

Then the browser will garbage collect after a certain size:

As you can see it went from the size to 4001 too 101. It looks like the browser doesn't bother garbage collecting the rest.

发布评论

评论列表(0)

  1. 暂无评论