How to understand closures and garbage collection mechanisms?

recently I have read articles about garbage collection mechanism and closures, but I still don"t have a deep understanding of them, and there are some doubts. I hope some bosses can give me an answer

.

my understanding is that local variables are recycled after the function is executed, while global variables are not recycled until the window closes (because of the life cycle of global variables? )

so will the function declaration be recycled? Recycle only variables? The following example:

function a(){
    var i = 1;
    function b(){
        return i
    }
    return b
}
var x = a();
x()

after executing x (), the function an and the internal variables I and b will be recycled?

Another function of

closures is to reside in memory, for example:

for(var i = 0; i< 9; iPP){
    (function(i){
        setTimeout(function(){
            console.log(i)
        },1000)
    })(i)
}

this example is to save the variable I in an anonymous function, so will I be recycled after the function is executed?


memory Management-tag-clear algorithm part

  • function declaration: the function declaration is the same as the variable, but if the global is still available, it will not be recycled. function fun () {}; fun = {}; then the memory that originally belonged to the function part is reclaimed.
  • The reference to the return value of the
  • function: x = a () , the last x is the internal function b, and the internal reference I of the function b, then b and I will not be recycled. After the execution of x () , x still maintains the reference to b globally, and b and I will not be recycled.
  • similarly, after the execution ends, the global I can still be obtained, so the global I will not be destroyed.

    • as I understand it, there are two I's here.
    • The
    • closure exists in the anonymous callback of setTimeout. The reference to the I of the anonymous function (IIFE) is executed immediately in the outer layer. The I of this layer is destroyed after execution. This is the argument I of IIFE;
    • another layer of IIFE is not a direct reference to the global I, nor a closure, but a simple parameter transfer. The relationship between the global I and the global I is only two variables with the same value at that time, retaining the value of the outer I at that time.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e5a47d-45594.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e5a47d-45594.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?