? Strong references in JavaScript: The reference of an object is a strong reference in JavaScript, that is, when将一个引用对象通过变量或常量保存
, then the variable or constant is a strong reference, and the object will not be Recycle.
Weak references in JavaScript: WeakMaps and WeakSets are the only ways we can use weak references in JavaScript.将一个对象作为键添加
to a WeakMap or WeakSet does not prevent these objects from being recycled.
. Strong reference is a child A holding a dog, and they are connected by a dog chain.
A weak reference is that there is a person B pointing to the dog held by A and saying: Hey, there is a dog there, and B points to the dog, but there is nothing tied together between them.
When A lets go of the dog leash, the dog will run away (was garbage collected), regardless of whether B is still pointing.
However, when B no longer points to the dog, the dog is still being held by A, which will not affect whether it runs away.
let people = {name:'Zhang San',age:25} let people1 = people;
In the above code, when we assign {name:'张三',age:25}
to the variable people, there will be a line connecting them in the memory:
Then create the people1 variable and assign people to people1, which is equivalent to people1 also referencing this object:
Let's take a look at what happens when we use the newly introduced types WeakSet and WeakMap in ES6 to store reference values.
let people = {name:'Zhang San',age:25} let people1 = people; let set = new WeakSet(); set.add(people);
We created a new WeakSet() instance and added people through the add method. The reference value corresponding to people is {name:'张三',age:25}
.
It can be seen that the value {name:'张三',age:25}
in the set instance refers to {name:'张三',age:25}
(in actual memory, it points to the stack of the data Pointer reference, the stack points to the value of the corresponding address in the corresponding heap). And it is important to note that this "line" of weak references is transparent. What does this mean? What is the difference between it and strong quotation?
One sentence summary: A strong reference is recognized as a "connection" by the reference {name:'张三',age:25}
, while a weak reference is not recognized. That is, the reference does not know that it is referenced by the set instance.
This means that garbage collection does not know that the reference is referenced by the set instance. Then if all strong reference connections of the reference are disconnected (the variable is assigned to null or other circumstances), then the reference will be destroyed as garbage, even if the set instance still refers to the reference.
let people = {name:'Zhang San',age:25} let people1 = people; let set = new WeakSet(); set.add(people); people = null; people1 = null;
What will happen if we disconnect all strong references?
Because all strong references are disconnected, garbage collection considers that the reference {name:'张三',age:25}
is no longer needed, and will destroy it. Then the reference used by the corresponding set instance no longer exists, even if the set instance is still using the reference.
1. The reference to the object in the WeakSet is a weak reference. That is to say, even if the weakset "references" an object, garbage collection does not count this reference as a "reference" as long as there are no strong references elsewhere. This object is unreachable and may be recycled at any time; it can only store reference types and cannot be enumerated or cleared.
2. WeakMap is similar to Map, but it is not enumerable, cannot be cleared, and the object referenced by key is a weak reference.
3. WeakSet is suitable for temporarily storing a group of objects and storing information bound to the objects. As long as these objects disappear externally, their references in the WeakSet will automatically disappear. Due to the above characteristics, members of WeakSet are not suitable for reference because they will disappear at any time.
4. Strong references sometimes forget to dereference, causing the memory to be unable to be released, which may cause memory leaks. Weak references are not included in the garbage collection mechanism, so this problem does not exist.