javascript - garbage collection with node.js -


I was curious that nodes of nested functions. How does JS pattern v8 work with garbage collector Function restofprogram (val2) {...} (val)})

  readfile ("blah", function (str) {var val = getvaluefromstr (str) code>  < P> If the restofprogram is running for a long time, does not it mean that str waste will never be collected? I understand that with nodes, you finish a lot with nested functions. Does this garbage collect? If the rest of the program is declared outside, then such an area can not be there. Is this a recommended practice?  

No I did not intend to complicate the problem, it was just careless, so I have modified it.

Simple Answer: If Str is not referenced from anywhere else (and is not referred to as str auto restofprogram ) function (str) {...} returns.

Description: The so-called context variable that was captured by the V8 compiler closure < Differentiate the em> local variable

When the execution of the function is complete, the local variables remain on the stack and disappear .

Reference variables reside in a stack allocated reference structure. The reference structure dies when they disappear. The important thing to note here is that the reference variables in the same structure from the same radius describe me with an example code:

  the function external () {Var x; // real local variables var y; // reference variable, which is referred to as internal 1 var z; // reference variable, the internal 2 function is referred to by inner1 () {// reference reference usage (y); } Function inter 2 () {// reference reference usage (z); } Function inter 3 () {/ * I am empty but I have still captured the reference / return [internal 1, inner 2, inner 3]; }     external  returns in this example but variable  y  and <<3> <3> <3> <3> <3> <3> <3> <3>  dead> Due because  y  and  z  the same reference structure And all three closures do not use this reference structure (even  inner3 ) to clearly explain it).  

When you start using with -Statement, try / catch -statment, the situation becomes more complex, on which V Position in 8th with a strong - Catch class or Global eval .

  function complexity () {var x; // reference variable function internally () {/ * I am empty, but I still try to capture the context)} {} hold (e) {/ * with built in discretion inherent in /}   

In this example, x will disappear only when internal dies because:

  • Try it - Join in the holder section
  • V8 assumes that any < - location shadow All local

    this force X exists to become a reference variable and x exists until << Code> x is present.

    In general, if you want to make sure that the given variable is not maintained, in fact for some time you need to specify some variable by null Em> this link can easily

Comments