Further locating memory leak with memwatch

2.2k views Asked by At

Recently I started my first project with node.js, and I can definitely say I'm loving it. Very powerful with all the modules; however, it seems I'm having a "slight" memory leak that causes my server to crash after about an hour (hit's 99-100% CPU). I've been trying to fix this problem for a while now.

Luckily, after a bit of searching, I found a popular tool called memwatch. I of course installed the module, and started logging memory usage/storage of my server's process.

Eventually, after looking through the logs, I have found the likely cause.

  {
    "what": "String",
    "size_bytes": 9421368,
    "size": "8.98 mb",
    "+": 16635,
    "-": 533
  }

Of course, within thirty seconds this little bugger managed an increase of 9mb (very unusual). This is nice and dandy to know that my memory leak seems to be of type string, but where exactly do I go from here? Is there any way I can get more accurate results?

I looked through my code, but there really isn't a string in my code that could possibly grow like this. Is there a possibility this string isn't actually a part of my code, and more a part of node or the Socket.IO module?

3

There are 3 answers

0
Shubhra On BEST ANSWER

Right approach. Use StrongOps (Previously Nodefly) to profile memory. Isolate type of leaking object. Look at heap retained sizes as well as the instance counts. Growing Instance counts with steady workload will point at few smoking guns.

I believe StrongOps uses memwatch + some V8 profiler/GC code under the hood. Better automation. See link - http://strongloop.com/node-js-performance/strongops/

Then used node-heapdump module, that their co-founder (core contributor Ben Noordhuis) wrote to isolate the leak down to collection object, GC roots and line of code.

See blog from Ben - http://strongloop.com/strongblog/how-to-heap-snapshots/

1
Andrei Karpuszonak On

You can use node-heapdump module, to make a dump of the V8 heap for later inspection, so you will be able to see more accurate results.

After you will make a heapdump, analyze it with Chrome DevTools:

https://developers.google.com/chrome-developer-tools/docs/javascript-memory-profiling

1
enducat On

As Shubhra suggested, another tool to consider in helping you diagnose your memory leak is the heap profiler of StrongOps monitoring. You can easily get started in a few steps here: http://docs.strongloop.com/display/DOC/Setting+up+StrongOps+monitoring

enter image description here

This will save you time from having to dig through logs and gives you a visual of what's going on in your applications heap over time, as well as comparing the String to other likely culprits causing your memory leak.

You can find more information here: http://docs.strongloop.com/display/DOC/Profiling#Profiling-Memoryprofiler