Linux – the formula to determine how much memory a socket consumes under Linux

linuxmemorytcp

I'm doing some capacity planning and I was wondering if there is a formula that I could use to predict (from a memory standpoint) how many TCP connections I could handle on my server. At the moment, I'm only concerned about memory requirements.

Some variables that I think will show up in the formula are:

  • sysctl's net.ipv4.tcp_wmem (min or default value)
  • sysctl's net.ipv4.tcp_rmem (min or default value)
  • the size of the sock, sock_common, proto and other per-socket data structures.

I'm not sure how much of the tcp_wmem and tcp_rmem are actually allocated and when that memory is allocated. At socket creation time? On demand?

Best Answer

If you can modify the source code, then use rusage data to measure the RSS and record how many TCP connections are in play at the time of the measurement.

If source code cannot be changed, then use the RSS of the network app as reported by top or ps and get the number of network connections at the time of measurement from lsof -i.

Collect this data every minute while your application moves through peak load, and from that data you can come up with a formula that relates number of connections to RAM usage.

Of course there are a lot more things that you could measure, in particular you might want to measure kernel RAM usage, although tcp data structures should be predictable and calculable in advance. In any case, have a look at this question https://serverfault.com/questions/10852/what-limits-the-maximum-number-of-connections-on-a-linux-server for more information on TCP tuning and how to get a clear view of what is happening in the network stack.

Related Question