javascript - Unclear about V8 Garbage Collection -
i having hard time wrapping head around garbage collected in node.js v8 , why (or why not). have example below using async.js library.
i love insight how v8 treats each case below (numbered 1 6 in comments).
thanks in advance.
"use strict"; var async = require('async'); module.exports = { foo: function foo(shirt, callback) { async.series([ function(next) { var pants = 'clown', outfit = shirt + pants; // 1. shirt garbage collected? next(null, outfit); }, function(next) { var err = new error('can\'t touch this.'), pants = 'hammer', outfit = shirt + pants; // 2. shirt garbage collected? setimmediate(function() { // 3. err garbage collected? // 4. outfit garbage collected? // 5. next garbage collected? next(err, outfit); }) } ], function(err, results) { // 6. callback garbage collected? callback(err, results); }); } };
both shirt (1, 2) , callback (3) referenced inner functions. bindings have heap-allocated, , become garbage when inner functions have become garbage. in example, happen after async.series has finished execution , dropped reference array in inner functions stored.
of course, both shirt , callback parameters. when foo drops references values, caller still have some, in case these values stay alive longer. cannot know locally.
similarly, err (3) , outfit (4) become garbage when inner function passed setimmediate, references them, becomes garbage (which happen after executed), , when whatever continuation next passes them not keep them alive longer. in example, passed on callback parameter don't know -- keep them alive indefinitely storing them away somewhere, or drop them immediately.
finally, next (5) argument again. value live @ least until setimmediate fires. function async.series creates, , know how long lives you'd know in detail how async.series handles (i assume drops immediately).
Comments
Post a Comment