When is this volume / closure garbage collection in javaScript?

I am doing a course that goes through volume / closure and briefly mentions garbage collection. During the course, the question is:

How long does the scope take? And the answer was - until there are no more references to it. Yes, so what we basically said was closing is like a reference to an object of a hidden scope. So for now, there is some function that still has the scope that the coverage will remain around. But as soon as this closure leaves, the volume may receive garbage collection. "

var sum = function sumHndlr(x, y) { if (y !== undefined) { return x + y; } else { return function(y) { return x + y; } } } 

So, the closing reference is when we assigned the function to the sum variable, but that does not mean that it lasts forever, or I don’t understand how js will be executed in relation to the parser compiler, etc.

+3
source share
1 answer

In short, garbage collection is the background process of the Javascript interpreter / virtual machine that automatically frees up memory for objects that your program no longer needs.

For example, since you are thinking about this for event listeners: when you delete an event listener (usually a function) from an event dispatcher, it is likely that no other part of the program will have links to the event listener. Therefore, the garbage collector can and will (at some unknown time) free up memory that was made by the event listener.

Since the closure refers to the object of the scope, in order to garbage collect the closure, it must be not only canceled, but also the area.

If you change your example a bit:

 /* some stuff ... */ function add10(a) { var adder = function (x) { return function(y) { return x + y; } } var sum = adder(10); //creates a closure return sum(a); } var thirty = add10(20); /* Some more stuff ... */ 

After calling add10 , although the program continues to do a few more things, the garbage collector can free the memory associated with closing sum because it no longer refers, and this is the area associated with it.

In this example:

 /* some stuff ... */ function add10AndGet20Adder(a) { var adders = function (x, y) { return [function(z) { return x + z; }, function(z) { return y + z; }] } var sums = adders(10, 20); //creates a closure return [sums[0](a), sums[1]]; } var result = add10AndGet20Adder(50); var sixty = result[0]; var add20 = result[1]; /* Some more stuff ... */ 

When performing some additional actions, sums[0] no longer referenced, BUT add20 ( sums[1] ) still has a reference to the region that refers to x and y , so neither of the two closures in sums can be freed by the garbage collector, until add20 is indicated by the program.

I hope this becomes clearer, although the examples, of course, are nowhere close to the real code. In practice, you only need to worry about this if you are developing a long-term program (for example, a single-page application or nodeJS server) with complex use of closures. Otherwise, using memory in closure is unlikely to be a problem.

This SO question gives more details on Javascript garbage collection: What is JavaScript garbage collection?

+1
source

Source: https://habr.com/ru/post/1271349/


All Articles