Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js RSS memory grows over time despite fairly consistent heap sizes

I've got a node.js application where the RSS memory usage seems to keep growing despite the heapUsed/heapTotal staying relatively constant.

Here's a graph of the three memory measurements taken over a week (from process.memoryUsage()):

memory graph

You may note that there's a somewhat cyclical pattern - this corresponds with the application's activity throughout each day.

There actually does seem to be a slight growth in the heap, although it's nowhere near that of the RSS growth. So I've been taking heap dumps every now and then (using node-heapdump), and using Chrome's heap compare feature to find leaks.
One such comparison might look like the following (sorted by size delta in descending order):

heap dump

What actually shows up does depend on when the snapshot was taken (eg sometimes more Buffer objects are allocated etc) - here I've tried to take a sample which demonstrates the issue best.

First thing to note is that the sizes on the left side (203MB vs 345MB) are much higher than heap sizes shown in the graph. Secondly, the size deltas clearly don't match up with the 142MB difference. In fact, sorting by size delta in ascending order, many objects have be deallocated, which means that the heap should be smaller!

Does anyone have any idea on:

  • why is this the case? (RSS constantly growing with stable heap size)
  • how can I stop this from happening, short of restarting the server every now and then?

Other details:
Node version: 0.10.28
OS: Ubuntu 12.04, 64-bit

Update: list of modules being used:

  • async v0.2.6
  • log4js v0.6.2
  • mysql v2.0.0-alpha7
  • nodemailer v0.4.4
  • node-time v0.9.2 (for timezone info, not to be confused with nodetime)
  • sockjs v0.3.8
  • underscore v1.4.4
  • usage v0.3.9 (for CPU stats, not used for memory usage)
  • webkit-devtools-agent v0.2.3 (loaded but not activated)
    heapdump v0.2.0 is loaded when a dump is made.

Thanks for reading.

like image 982
zinga Avatar asked Jun 30 '14 00:06

zinga


1 Answers

The difference you see between RSS usage and heap usage are buffers.

"A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap" https://nodejs.org/api/buffer.html#buffer_buffer

like image 138
Sonny Piers Avatar answered Sep 30 '22 17:09

Sonny Piers