Garbage collection mechanism of weakSet

Posted by jlryan on Sat, 04 Apr 2020 13:07:24 +0200

WeakSet object

Difference: WeakSet is similar to Set in that it is a de reordered aggregation of data. But there are two differences:

  1. A WeakSet member can only be an object, not data of other types.
  2. The WeakSet member objects are all weak references, that is, the garbage collection mechanism will not consider the reference of the WeakSet object to the object.

If other objects do not reference the object, the garbage collection mechanism will automatically recycle the memory occupied by the object, regardless of whether the object is still in the WeakSet object. Because of the above characteristics, the member objects in the WeakSet will disappear at any time (the garbage collection mechanism may be different before and after operation)

Syntax: new WeakSet([iterable])

  • iterable array, class array (or data that implements iterator interface)

Brief table of WeakSet instance method: Method description :-:|--- add() | add a member to the WeakSet delete() | delete a member from the WeakSet has() | judge whether a member exists in the WeakSet b

Note: because of the weak reference feature of WeakSet, we can use it to store DOM, so we don't have to worry about the DOM being deleted by the page and not leaking in memory.

Create a weakset

      //Create a weakSet
        let ws = new WeakSet([["1","2",3]])
        let arr = [1,3,4]
        // null is also illegal
        let ws2 = new WeakSet([[1,2],{color:"red"},()=>{},arr])
        console.log(ws,ws2)

Method of WeakSet

 //= = = add
        let arr = [1,3,4]
        let ws2 = new WeakSet([[1,2],{color:"red"},()=>{},arr])
        ws2.add(window).add(location)
        console.log(ws2)
        //delete
        // let ws2 = new WeakSet([[1,2],{color:"red"},()=>{},arr])
        console.log(ws2.delete(window))//true
        console.log(ws2.delete(window))//false
        //Judge whether it exists
        console.log(ws2.has(window))//false
        console.log(ws2.has(location))//true
        console.log(ws2)

WeakSet garbage collection mechanism

// Define objects
// let ickt = [];
let ws = new WeakSet();
// class
class Demo {
    // Constructor
    constructor(color) {
        this.color = color;
        // Store instance objects
        // ickt.push(this)
        // Using ws to store this avoids references to instances
        ws.add(this)
    }
    // Get color method
    getColor() {
        // if (ickt.indexOf(this) === -1) {
        //     //Throw error
        //     throw new Error('This object is not an instance of Demo, this method cannot be used ')
        // }
        if (!ws.has(this)) {
            // Throw wrong
            throw new Error('The object is not Demo Cannot use this method')
        }
        return this.color;
    }
}
let d1 = new Demo('red')
let d2 = new Demo('green')
let obj = { color: 'pink' }
// Delete D2. The original instance of D2 should be processed by memory. But they didn't deal with it
d2 = null;
// get colors
console.log(d1.getColor());
// Let this point to d2
console.log(d1.getColor.call(d2));
// Make this point to obj
console.log(d1.getColor.call(obj));

Topics: Front-end