Memory pools with Node.js

95 views
Skip to first unread message

Manjula Peiris

unread,
Jun 28, 2015, 12:55:06 PM6/28/15
to nod...@googlegroups.com
Hi,

I am currently checking whether we can improve the Node.js runtime (server) performance by reducing the number of calls to "new" and "delete" to allocate/deallocate dynamic memory. For example I am looking for something similar to memory pools used in Apache HTTPD (http://www.apachetutor.org/dev/pools). If you have any pointers please let me know.

Thanks,
Manjula.

Jimb Esser

unread,
Jul 1, 2015, 11:39:30 PM7/1/15
to nod...@googlegroups.com
Most actual calls to "new" and "delete" will be happening in V8, and I would assume they already make heavy use of memory pools and similar methods.

On the Javascript side as well, doing pooling can go a long way to improving performance and reduce the amount of time spent in object initialization and the garbage collector.  Especially with objects like what backs the Buffer class, where allocating one of those in Javascript requires jumping into native code, using pooling can help tremendously (and, in fact, Node's Buffer objects are relatively light-weight wrapper objects around a pool of SlowBuffer objects, so this is already happening).

In previous projects we've found pooling to greatly help when we had an object with native components (such as a large Buffer, or custom native classes), and also found it to help even with pure-JS objects which did any significant setup/allocations in their constructors.  If you're making thousands of some kind of objects a second, then it may benefit from being pooled so that it is not constantly generating garbage.  However, the generational garbage collector in V8 is a complicated and finicky beast and this can have adverse effects as well, since these pooled objects and the things they reference will be in "old" generations which are not garbage collected as efficiently and could slow down overall performance.  It's hard to know whether or not pooling will actually speed up a given situation, so the only thing to do is, if it seems like something is generating a lot of garbage, try adding pooling and measure how the overall application performance changes (due to the nature of the garbage collector, the performance increase/decrease will likely show up in unrelated parts of the application, so just profiling the changed code will not be as accurate as measuring the entire application's performance).

  Jimb Esser
Reply all
Reply to author
Forward
0 new messages