Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection on Map and WeakMap collections in es6

I was reading the WeakMap's description and it said:

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.

From reading this, my understanding is that WeakMaps are used in instances where you're trying to take advantage of JavaScript's garbage collection. 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? What's the advantage to this, and do I have to explicitly delete from normal Maps?

like image 217
m0meni Avatar asked Dec 12 '15 01:12

m0meni


1 Answers

"Why, in a normal Map, would objects with no references to them not be garbage collected?" because the map itself has a reference to the object. As long as there is a reference, the object is not gced. - Felix Kling

like image 149
m0meni Avatar answered Sep 23 '22 19:09

m0meni