Reading Node.js Hap Snapshots (Created via Nodetime) - Why Are My Objects Not GC'd?

I have some unexpected results in my Nodetime heap snapshot:

enter image description here

Am I reading this right, that in my heap are 1706 instances of "user"? It seems absurdly tall, I'm not even sure how I would single it out a lot. Anyway, are there any tips / hints / tips to find out why they hang around? I have already used JSHint for my code to ensure that no free global blocks will be allocated. Everything should be wrapped up in the request closing volume ... so why users, messages, etc. Do not collect garbage when the request ends? Here is some (edited) code to show how I do things ...

What is surprising is that I took a snapshot with a bunch of about 10 m after the last API call . Thus, these objects hung around all the time after the queries that caused their selection were completed!

the code:

var User = require('./user').User, Q = require('q'); // this function is called via express' router, eg when the client visits myapi.com/users/zane function getUser(req, res, next) { var user = extend({},User).initialize(); Q.ncall(user.model.find, user.model, {'username': req.arguments[0]}) .then(function(data){ res.writeHead(200, {}); res.end(JSON.stringify(data)); }) .fail(next).end(); } 

And the User module looks something like this:

 exports.User = extend({}, { initialize: function() { var Schema = api.mongoose.Schema; this.schema = new Schema({ 'username': {'type':String, 'index':true} }); this.model = api.db.model('users', this.schema); } // ... some other helper functions in here }); 

Based on my express code above, I expect that the lifetime of the user object will only be allocated until the request is returned. Is there any key idea of ​​Node.js GC here?

+5
Oct. 14 '12 at 14:56
source share
1 answer

This line looks suspiciously inefficient:

 var user = extend({},User).initialize(); 

I assume that calling extend copies the User object and then calls its initialize method. Why do you copy a User object every time you call the API?

Then, in the initialize call, you create a new Mongoose schema object and then register it as a model using the api.db.model call. Wouldn't it be better to create the circuit once and register it during initialization?

The combination of both of them can lead to the creation of more objects with each call than necessary, and I am sure that registered registered Mongoose models are not GC easily.

+2
Oct 14
source share
β€” -



All Articles