This is the 7th day of my participation in the August Text Challenge.More challenges in August

In JS, for any variable, object, array, instance and so on these, will consume our memory and resources, in order to save resources and improve speed, in JS, he is how to deal with the variable array we no longer use?

What is a memory leak

The running of the program needs memory, the various operations in the program running need to consume resources and memory, and the various data generated during the program running also need memory. If the memory is not released in time, the memory usage becomes higher and higher, which may affect the performance of the program and the system at a light level, or cause the process or the system to crash. A memory leak is called when no longer used memory is released. For some languages, automated memory management is called garbage collection

Second, what is “garbage”?

JS’s garbage collection mechanism periodically (periodically) finds memory (variables) that are no longer needed and then frees it. Quite simply, content that we no longer use is disposed of as garbage. Values that are no longer used are garbage collected. (Note accessibility)

  • Useful values:
    • Local variables and arguments of a local function
    • Call the variables and arguments of other functions on the chain
    • The global variable
    • Accessible value
  • Useless values:
    • A value whose address or value is null
    • Variables and parameters in the local scope of a function (after the function is complete)

And so on and so on.

Three, JS recycling method

1. Mark clear

This is the most common method of garbage collection in JS. It works by marking all variables, then tracing back to their references and marking them again, and so on until there are unaccessed (unreachable) references, and all but the marked objects are deleted.

  • No object is marked twice
  • Access the start tag from the root

Photo credit: Sifu. Front-end wisdom)

This is an object structure:

Look at the flow of the garbage collection mechanism

First, mark from the root:

Second, mark their references:

Third, mark references to their descendants, and so on:

Fourth, all objects except the marked object are deleted:

This is how the garbage collection mechanism works. There are many optimizations for this in JS:

  • Generational recycling: tag on object can be divided into two groups: “new object” and “old object,” for a new object, is often appear and fast handling repetitive tasks, this object will be regularly check and soon is cleared, and the old object, is those who survive for very long, the less the number of inspection would be.
  • Incremental collection: If the number of objects is large, it will take some time to traverse the entire object set at once, and there will be some delay in execution. Therefore, the JS engine tries to break up the garbage collection mechanism and execute it separately in order to reduce latency.
  • Idle time mobile: The garbage collector only runs when the CPU is idle, reducing the possibility of impact on execution

2. Reference count

Based on the number of citations. When declaring a variable and will be a reference type assigned to the variable, this is worth reference number is + 1, on the contrary, if contain reference to the value of variables and made another value, it is worth reference number is 1, when this is worth reference number into 0 means that there is no way to access the value again, So you can reclaim the memory space it takes up. In this way, the next time the garbage collector runs, it will free the memory occupied by those zero references. But there are some problems with this:

function problem(){
    var objA = new Object(a);var objB = new Object(a); objA.someOtherObject = objB; objB.someOtherObject = objA; }Copy the code

In the above code, objA and objB refer to each other by their respective attributes, and their references will always be 2. In the reference-counting strategy, after the function is executed, var is used, and objA and objB will still exist, causing memory leaks. Their circular references need to be cut manually.

Four,

For the JS garbage collection mechanism, the less the better, of course, the less garbage generation is the better, we can reduce the garbage collection by some methods, such as manual memory free:

let arr = [a,b,c,d,e,f];
arr.length;        // Set length to 0 to free memory
let obj = {
    name: "Xiao Ming".age: 18,
}
obj = null;        // Clear references to free memory
// Wait...
Copy the code

I hope you can point out any questions in the comments section, AND I will correct them in time.

New people on the road, but also include more. I am MoonLight, a fledgling small front end.