Using the code snippet verbatim on the top-level (i.e. not nested within a closure) will make "s" be bound to a property on the global object. Since the global object is shared between all scripts running in the same context, there is no way for V8 to determine a limited lifetime for this variable and the string will be kept alive forever.
Best regards, Michael On Wed, Jun 11, 2014 at 10:24 AM, Mihail Slavchev <[email protected] > wrote: > Hi all, > > I am reading V8 source code in order to get better understanding how V8 > maintains object lifetime during GC. Here is a short example that I am > trying to understand. > > var s = "hello " > > (function(name) { > console.log(s + name) > })("John") > > Obviously, the GC won't collect the string "hello " before executing the > function because it is referred from within the function via the "s" > variable. I guess V8 creates a temporary object on the fly that actually > has a property, say "__s", that holds a reference to the "hello " string. > > Is that right? I will appreciate if someone points me to the right place > in V8 source code where I can learn the details. > > Thanks in advance, > Mihail > > -- > -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users > --- > You received this message because you are subscribed to the Google Groups > "v8-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
