When was the closure destroyed?

when was the closure destroyed? Will it cause a memory leak?
there are many theories on the Internet about whether closures will cause memory leaks. Is there any great god who can answer it?

Jul.22,2021

The

closure is actually the capture of the variables in the parent scope by the function. If the function is in, the closure is in, the function is not referenced, and the closure is destroyed.

function test() {
  var data = new Array(100000); 
  var getData = function(){return data;};
  setTimeout(getData, 10000);
}

generally speaking, local variables created within a function are automatically destroyed after the function has finished running. In the example, each time the test function is run, the data data is created. If the getData does not refer to the data variable in the upper scope, the data will be destroyed after the test function finishes running. When getData is created, a special container is created to hold references to variables in the upper scope. It can be said that the closure created by the getData function captures references to external data variables. In theory, the variable getData will also be destroyed after the test function finishes running. This is also true, if there is no later setTimeout. Because setTimeout has been holding the reference to the getData function, and the closure formed by getData captures the reference to the data variable, the data data will always exist and will not be destroyed immediately after the end of the test function. SetTimeout runs the function pointed to by getData after 10 seconds, and then releases the function reference, that is, there are no variables referencing the function pointed to by getData after 10 seconds, then the closure formed by getData can also be destroyed, and the captured upper variables are also released. So in the example, the closure formed by getData is destroyed after 10 seconds.

then why is there a memory leak?

imagine that if you are not using setTimeout but setInterval, in the example, the function getData has been referenced in setInterval, and the closure formed by getData and the variables captured by the closure will remain until you clearInterval. If you forget clear, or if you mistakenly set up multiple timers but don't know that only the last one is clear, there is a memory leak.

in short, as long as no one saves the reference to the function, the function and the closure formed by the function will be destroyed as well.


first of all, we need to correctly understand what a closure is. Generally speaking, a closure is a function set of functions, but it does not necessarily satisfy that a function set function is a closure. So in the long-term summary process, there is another view: the first-level function calls the second-level function, and the second-level function accesses the first-level function lexical scope variable is called the closure. The closure will cause memory leakage, which has always been a drawback of the closure.

Menu