Linux 内核每个 TCP/IP 网络连接消耗多少内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8646190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:50:06  来源:igfitidea点击:

How much memory is consumed by the Linux kernel per TCP/IP network connection?

linuxmemory-managementtcp-ip

提问by

How much memory on average is consumed by the Linux kernel (in kernel address space) per TCP/IP network connection?

Linux 内核(在内核地址空间中)每个 TCP/IP 网络连接平均消耗多少内存?

采纳答案by daya

For a TCP connection memory consumed depends on

对于一个 TCP 连接消耗的内存取决于

  1. size of sk_buff (internal networking structure used by linux kernel)

  2. the read and write buffer for a connection

  1. sk_buff的大小(linux内核使用的内部网络结构)

  2. 连接的读写缓冲区

the size of buffers can be tweaked as required

缓冲区的大小可以根据需要调整

root@x:~# sysctl -A | grep net | grep mem

check for these variables

检查这些变量

these specify the maximum default memory buffer usage for all network connections in kernel

这些指定内核中所有网络连接的最大默认内存缓冲区使用量

net.core.wmem_max = 131071

net.core.rmem_max = 131071

net.core.wmem_default = 126976

net.core.rmem_default = 126976

these specify buffer memory usage specific to tcp connections

这些指定特定于 tcp 连接的缓冲区内存使用

net.ipv4.tcp_mem = 378528   504704  757056

net.ipv4.tcp_wmem = 4096    16384   4194304

net.ipv4.tcp_rmem = 4096    87380   4194304

the three values specified are " min default max" buffer sizes. So to start with linux will use the default values of read and write buffer for each connection. As the number of connection increases , these buffers will be reduced [at most till the specified min value] Same is the case for max buffer value.

指定的三个值是“min default max”缓冲区大小。所以从linux开始,每个连接都会使用默认的读写缓冲区值。随着连接数量的增加,这些缓冲区将减少 [最多直到指定的最小值] 最大缓冲区值的情况也是如此。

These values can be set using this sysctl -w KEY=KEY VALUE

可以使用此设置这些值 sysctl -w KEY=KEY VALUE

eg. The below command ensures the read and write buffers for each connection are 4096 each.

例如。以下命令确保每个连接的读取和写入缓冲区均为 4096。

sysctl -w net.ipv4.tcp_rmem='4096 4096 4096'

sysctl -w net.ipv4.tcp_wmem='4096 4096 4096'

回答by ugoren

It depends. On many many things.
I think an idle connection will take a few hundreds of bytes.
But if there's data in the transmit and/or receive data, then the consumption increases. The window size can roughly limit this consumption.
The extra consumption for data isn't just the bytes in the receive/transmit queue. There are overheads, so a segment with one byte might take something like 2K. TCP tries to reduce this, for example by merging segments into a single sk_buff, but it doesn't always succeed.

这取决于。在很多很多事情上。
我认为空闲连接将占用数百个字节。
但是如果传输和/或接收数据中有数据,那么消耗就会增加。窗口大小可以大致限制这种消耗。
数据的额外消耗不仅仅是接收/传输队列中的字节。有开销,所以一个字节的段可能需要像 2K 这样的东西。TCP 试图减少这种情况,例如通过将段合并为单个 sk_buff,但它并不总是成功。

回答by j?rgensen

Also depends on which layer. In case of a pure bridging scenario, there's just the bridge-level FDB. When routing comes into play, there's the routing table and the IP-level FDB/neighbor db. And finally, once a local socket is in the play, you have of course window size, socket buffers (both send and receive, and they default to 128k last time I checked), fragment lists (if used), so that is where your memory goes, but a clear-cut answer is hard to make with all the parts in use. You can use ss -mto obtain a few memory statistics of local stream sockets.

也要看是哪一层。在纯桥接场景的情况下,只有桥级 FDB。当路由发挥作用时,有路由表和 IP 级 FDB/邻居数据库。最后,一旦本地套接字在运行,你当然有窗口大小、套接字缓冲区(发送和接收,我上次检查时默认为 128k)、片段列表(如果使用),所以这就是你的记忆犹新,但很难对所有使用的部件做出明确的回答。您可以使用ss -m获取本地流套接字的一些内存统计信息。