joyent/node — pull request #3024 by yosefd (2012-03-28 09:41:14+00:00)
Title: Prevent excessive memory allocation by TLS (sometimes led to node crash) 

The problem:

When data is streamed via TLS, we’ve noticed extremely big increase in node memory size. It was particularly severe when data was sent in small fragments (e.g. HTML content built by writing multiple chunks into response body). Sometimes the memory grew to such vast amounts, that it was surpassing the virtual address space size and node was crashing.

The analysis:

We have found out that while pumping, TLS allocates 64KB buffers for each, even tiny chunk and even if there is no data at all (while just checking if new data is available). For chunks of a few bytes several 64KB buffers were allocated.
During the data streaming garbage collector sometimes didn’t have opportunity to kick in before memory grew beyond maximum and process was crashing (on Windows).

The solution:

Instead allocating new 64KB buffer each time when checking if there is something to transform, continue to use the same buffer. Once the buffer is exhausted, allocate new buffer. This solves the problem of huge allocations when small fragments of data are processes, but also will continue to work well with big pieces of data.

Authors: Elad Ben-Israel @eladb Yosef Dinerstein @yosefd
Credits: Saar Yahalom @saary