Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js memory leak, despite constant Heap + RSS sizes

Tags:

node.js

According to my server monitoring, my memory usage is creeping up over time:

enter image description here

After ~4 weeks of uptime, it ends up causing problems / crashing (which makes sense, given that I'm on EC2 with m1.large instances => 8GB RAM, and RAM seems to be increasing at about 1.5 GB / week).

If I restart my node.js app, the memory usage resets. Yet... I'm keeping track of my memory usage via process.memoryUsage(), and even after ~1 week, I'm seeing

{"rss":"693 Mb","heapTotal":"120 Mb","heapUsed":"79 Mb"}

What am I missing? Clearly the leak is in node, yet the process seems to not be aware of it...

like image 321
Zane Claes Avatar asked Feb 05 '13 18:02

Zane Claes


People also ask

How do you avoid memory leaks in Node JS?

Avoid Accidental Globals This could be the result of a typo and could lead to a memory leak. Another way could be when assigning a variable to this within a function in the global scope. To avoid issues like this, always write JavaScript in strict mode using the 'use strict'; annotation at the top of your JS file.

How do you investigate memory leaks in Node JS?

How To Detect. There're many tools and libraries used to detect memory leaks in NodeJS, all following the same concept to detect memory leaks by comparing different heap dumps and check the results, and they try to force run the garbage collector before taking any heap dump to make sure that the leakage is real.

Does setInterval cause memory leak?

setTimeout and setInterval are the two timing events available in JavaScript. The setTimeout function executes when the given time is elapsed, whereas setInterval executes repeatedly for the given time interval. These timers are the most common cause of memory leaks.


1 Answers

You can try node-memwatch module, which helps with leak detection and heap diffing in Node.

Heap diff would look similar to:

{
  "before": { "nodes": 11625, "size_bytes": 1869904, "size": "1.78 mb" },
  "after":  { "nodes": 21435, "size_bytes": 2119136, "size": "2.02 mb" },
  "change": { "size_bytes": 249232, "size": "243.39 kb", "freed_nodes": 197,
    "allocated_nodes": 10007,
    "details": [
      { "what": "String",
        "size_bytes": -2120,  "size": "-2.07 kb",  "+": 3,    "-": 62
      },
      { "what": "Array",
        "size_bytes": 66687,  "size": "65.13 kb",  "+": 4,    "-": 78
      },
      { "what": "LeakingClass",
        "size_bytes": 239952, "size": "234.33 kb", "+": 9998, "-": 0
      }
    ]
  }
like image 132
Andrei Karpushonak Avatar answered Oct 21 '22 08:10

Andrei Karpushonak