8

I have some code that is working, however it has a memory leak in it.

What are some good strategies for tracking memory leaks in node.js?

What steps should I follow when looking for such leaks?

How can I track the leak in my code?

Thanks

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
BenoitD
  • 525
  • 1
  • 6
  • 16

1 Answers1

17

You can figure this out by profiling the memory usage of your application.

Javascript objects are allocated on the heap, so you'll want a tool that can dump the heap. After acquiring a heap dump you can inspect it and see how many instance of a given object (or function) exist.

E.g., for your code you know you create a socket whenever a user connects. Dumping the heap while three users are connected should show ~3 sockets. Dumping the heap after those users disconnect should show ~0 sockets.


You can actually use the Chrome heap dump analyzer with Node.js heap dumps.


Just fyi, functions are going to show up in the heap dump under the (closure) section.

You'll want to make sure you name your functions (even if they don't need a name) so they show up as something useful in the heap dump.

For example, something like

function() { }

will just show up as function() in the heap dump. Where as:

function taggedFunction() { }

will show up as function taggedFunction() in the heap dump. If you create 100 taggedFunctions then you'll see taggeFunction in the heap dump 100 times. Basically, naming your functions lets you figure out if you keep creating and leaking them.

Matt Wonlaw
  • 12,146
  • 5
  • 42
  • 44
  • 1
    +1 for a great answer with good links and constructive criticism on OP's code – Morten Jensen Mar 30 '13 at 21:25
  • The setInterval should be common to every connections. It is used to have the same time displayed for each user. Maybe there is another way to do it ? – BenoitD Mar 30 '13 at 21:28
  • Ah. Yeah, you're setInterval is actually fine. I didn't see that the `interval` variable was in the global scope and only ever got set once. I thought an interval was being created for each connection. – Matt Wonlaw Mar 30 '13 at 21:30
  • My script takes 1.5GB in memory with 100 clients connected to it. – BenoitD Mar 30 '13 at 21:31
  • Could it be socket.io leaking ? http://jpallen.net/2013/03/08/tracking-down-a-memory-leak-in-node-js-and-socket-io/ – BenoitD Mar 30 '13 at 22:27
  • Nothing jumped out at me as potentially causing a leak in the code you posted so socket.io could be the culprit. I guess another thing you could try is backing up your code and then cutting out pieces of functionality until either a. the leak goes away or b. the only thing left is basic socket.io stuff then you'll know where the leak is coming from. – Matt Wonlaw Mar 30 '13 at 22:34
  • I did a heapdump but can't find anything relevant. You can have a look at it here : https://docs.google.com/file/d/0Bwo6gMXJqjvwSy04dFJFbzdURGc/edit?usp=sharing – BenoitD Apr 02 '13 at 10:47
  • Using node --expose-gc myscript.js and calling global.gc() when a user disconnects I go from 50Mo to 33Mo. – BenoitD Apr 02 '13 at 11:07
  • 1
    I switched from socket.io to socks.js and I don't have memory leak anymore. – BenoitD Apr 11 '13 at 06:20