function(foo, cb) { var bigObject = new BigObject(); doFoo(foo, function(e) { if (e.type === bigObject.type) { cb();
The above example shows a classic, random (or maybe not) memory closure. The V8 garbage collector cannot determine if bigObject can be safely removed because it is used in a callback function that can be called multiple times.
One solution is to set bigObject to null when the job in the callback function is complete. But if you use a lot of variables (imagine there are n variables like bigObject and they are all used in the callback), then cleaning up becomes an ugly problem.
My question is this: is there any other way to clear the variables used?
EDIT Here is another example (real world): so I get the application from mongodb and compare it with some other application. The callback from mongodb uses a variable application that is determined from this callback. After receiving the result from mongodb, I return it as a callback as well (because it's all async, and I can't just write return). So it may actually happen that I extend the callback to the source ...
function compareApplications(application, condition, callback) { var model = database.getModel('Application'); model.find(condition, function (err, applicationFromMongo) { var result = (applicationFromMongo.applicationID == application.applicationID) callback(result) } }
ivan_zd May 08 '13 at 13:36 2013-05-08 13:36
source share