I'm relatively new to node.js, and I've been tasked with writing a simple message queue app - basically, the app will receive a post containing some message data and the client's mac address, which we decode and then insert into a sqlite database, to be posted later by a separate process.
Everything works fine, but we're experiencing what may be a memory leak. Using the memwatch module, I've narrowed the leak down to our code handling incoming posts, which I've included below:
app.route('*')
.post(function(req, res){
var mac = decodeURI(unescape(req.param('mac').replace(/\+/g, "%20")));
var data = decodeURI(unescape(req.param('data').replace(/\+/g, "%20")));
req = null;
db.run("INSERT INTO queue (mac, data) VALUES (?,?)", [mac, data], function(er){
if(er) {
res.writeHead(500, "Insert to queue failed", {'Content-Type': 'text/html'});
res.end();
} else {
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.end();
}
res = null;
mac = null;
data = null;
});
})
I'm unable to find anything in here that looks incorrect, but when we post 10000 requests to the app we almost always trigger a memory leak event in memwatch. I'm looking for a nudge in the right direction here: are we simply misinterpreting memwatch, or is there a leak here I'm missing somehow? As I understand it memwatch considers heap growth over 5 or more V8 garbage collection attempts consecutively to be a leak - it seems possible that with 10000 posts at once like this that that sort of heap growth would be expected, but I haven't yet found a definitive answer and need to rule it out. Any help, even if you just point me in the right direction, would be greatly appreciated!