KKK
KKK

Reputation: 1085

NodeJS application with memory leak, where is it?

I have a NodeJs application that listens to messages via subscribe on a Redis server. It collects the messages for a period of 5 Seconds and then pushes them out to the connected clients, the code looks something like this:

io.sockets.on('connection', function (socket) {
    nClients++;
    console.log("Number of clients connected " + nClients);
    socket.on('disconnect', function () {
        nClients--;
        console.log("Number of clients remaining " + nClients);
    });
});

Receiving messages to send out to the clients

cli_sub.on("message",function(channel,message) {
        oo = JSON.parse(message);
        ablv_last_message[oo[0]["base"]+"_"+oo[0]["alt"]] = message;
});

setInterval(function() {
    Object.keys(ablv_last_message).forEach( function(key) {
        io.sockets.emit('ablv', ablv_last_message[key]);
    });
    ablv_last_message = [];
}, 5000);

SOLUTION FOUND (at least I think so): Node didn't crash because it reached some internal memory limits, it looks as if it crashed because my VPS ran out of memory, it was a 2GB VPS running one or two other processes too. After upgrading it to 4GB, Node runs smoothly, yes always around 1.6 to 2.0 GB but I believe its the GC who does its work here.

Upvotes: 3

Views: 2551

Answers (3)

Damodaran
Damodaran

Reputation: 11065

It is better you try some tools for finding leaks in node.js.

Tools for Finding Leaks

  • Jimb Esser’s node-mtrace, which uses the GCC mtrace utility to profile heap usage.
  • Dave Pacheco’s node-heap-dump takes a snapshot of the V8 heap and serializes the whole thing out in a huge JSON file. It includes tools to traverse and investigate the resulting snapshot in JavaScript.
  • Danny Coates’s v8-profiler and node-inspector provide Node bindings for the V8 profiler and a Node debugging interface using the WebKit Web Inspector.
  • Felix Gnass’s fork of the same that un-disables the retainers graph Felix Geisendörfer’s Node Memory Leak Tutorial is a short and sweet explanation of how to use the v8-profiler and node-debugger, and is presently the state-of-the-art for most Node.js memory leak debugging.
  • Joyent’s SmartOS platform, which furnishes an arsenal of tools at your disposal for debugging Node.js memory leaks

From Tracking Down Memory Leaks in Node.js – A Node.JS Holiday Season.

And another blog

Upvotes: 7

simonleung
simonleung

Reputation: 44

You may use Object.getOwnPropertyNames rather than Object.keys

Upvotes: 0

Aaron Silverman
Aaron Silverman

Reputation: 22655

It looks to me that you keep adding keys to the global ablv_last_message object and never clean it.

Upvotes: 0

Related Questions