joyent/node — Issue #2435: Excess memory allocation in uv/UDP
Author: avz
Created: 2011-12-29 11:25:41+00:00
State: open

Functions ``unix/uv__udp_recvmsg()`` and ``win/uv_process_udp_recv_req()`` allocates 64Kb for each UDP packet regardless it real length:

```c
buf = handle->alloc_cb((uv_handle_t*)handle, 64 * 1024);
```
in addition, allocation is not needed if error occurred.
Maybe I also found memory leak because of this at ``src/udp_wrap.cc:351`` near

```c
if (nread == -1) {
// maybe need to add ReleaseMemory(buf.base, NULL); here, because buf allocates in all cases
SetErrno(uv_last_error(Loop()));
}
```
this is a very rare situation, so that is not noticeable.

Excess allocation leads to large memory consumption, in the case of a large number of small UDP-packets. For example, node.js-process, which just receiving 3000 100-bytes packets per second for 5 minutes uses 1 - 2+ Gb RSS and growing more and more.
I don't know why GC doesn't collect this 64-kb buffer, but I fixed allocation to real packet length (temporary local buffer, then cb_alloc() and memcpy()): memory consumption became normal (20 - 60Mb) and no more grow.