How to track object inside heap in node.js to find memory leak?

I have memory leak, and I know where is it (I think so), but I don't know why it is happening. Memory leak occurs while load-testing following endpoint (using restify.js server):

server.get('/test',function(req,res,next){
    fetchSomeDataFromDB().done(function(err,rows){
        res.json({ items: rows })
        next()
    })
})

I am pretty sure that res object is not disposed (by garbage collector). On every request memory used by app is growing. I have done some additional test:

var data = {}
for(var i = 0; i < 500; ++i) {
    data['key'+i] = 'abcdefghijklmnoprstuwxyz1234567890_'+i
}
server.get('/test',function(req,res,next){
    fetchSomeDataFromDB().done(function(err,rows){
        res._someVar = _.extend({},data)
        res.json({ items: rows })
        next()
    })
})

So on each request I am assigning big object to res object as its attribute. I observed that with this additional attribute memory grows much faster. Memory grows like 100Mb per 1000 requests done during 60 sec. After next same test memory grows 100mb again, and so on. Now when I know that res object is not "released" how I can track what is still keeping reference to res? Let say I will perform heap snapshot - how I can find what is referecing res?


screenshot of heap comparison between 10 requests:

enter image description here

Actually it seems that Instance.DAO is leaking?? this class belongs to ORM that I am using to query DB... What do you think?

One more screen of same coparison sorted by #delta:

enter image description here

It seems more likely that the GC hasn't collected the object yet since you are not leaking res anywhere in this code. Try running your script with the --expose-gc node argument and then set up an interval that periodically calls gc();. This will force the GC to run instead of being lazy.

If after that you find that are leaking memory for sure, you could use tools like the heapdump module to use the Chrome developer heap inspector to see what objects are taking up space.