[En-Nut-Discussion] Too many very fast opened / closed TCP connections may let the system run out of memory

Ole Reinhardt ole.reinhardt at embedded-it.de
Fri Jan 10 18:59:01 CET 2014

Hi all,

the third mail to a similar topic :-)

During my reliability tests with the TCP statemachine, I discovered a
potential problem.

When creating sockets very fast and directly closing them again, as soon
as the connection is established may let the Nut/OS device run out of

Test setup:

I build an application, that opens TCP connection to a PC, send out a
short string and directly close this connection again.

On the PC side, I started ncat (from nmap package) to open a listening
TCP port and dump the incomming data:

ncat --recv-only -l -k 2500

On Nut/OS I doo:

for (;;) {
  sock = NutTcpCreateSocket();
  if (NutTcpConnect() == 0) {
      /* send a short string */


If you have a fast network connection, the available heap memory on your
Nut/OS device rapidly decreases until 0.


As soon as the TCP socket is closed, the socket will not be destroyed
directly, but the TCP statemachine changes the socket state to
TCPS_TIME_WAIT. This is totally correct.

See http://tools.ietf.org/html/rfc793

The socket can now stay in TIME_WAIT for some time (up to a 4 * MSL
(maximum segment lifetime), before it is destroyed.

If we open new sockets fast enough this means, that the sockets,
currently waiting in TIME_WAIT state is accumulating quite fast. As each
socket is eating up >100 bytes of memory, memory may go low quite fast.


In reality this won't be a problem. The problem only occurs, if new
sockets are created very very fast (each 3-5ms in my tests).

But we should think about, if there may be any solution to limit the
memory usage, while still correctly implementing the TCP standard

Unfortunately I do not have a good idea yet.

Best regards,


kernel concepts GmbH            Tel: +49-271-771091-14
Sieghuetter Hauptweg 48         Mob: +49-177-7420433
D-57072 Siegen

More information about the En-Nut-Discussion mailing list