the socket buffer size of linux


# Most versions of UNIX have a very low default buffer limit, which should be increased to at least 2MB. Also, note that UDP recommendations are only applicable for configurations which explicitly configure UDP in favor of TCP as TCP is the default for performance sensitive tasks.

# for read
cat /proc/sys/net/ipv4/tcp_rmem
# for write
cat /proc/sys/net/ipv4/tcp_wmem

sysctl net.ipv4.tcp_rmem
sysctl net.ipv4.tcp_wmem

sysctl -w net.core.rmem_max=2097152
sysctl -w net.core.wmem_max=2097152


# However, the default socket buffers are just set when the sock is initialised but the kernel then dynamically sizes them (unless set using setsockopt() with SO_SNDBUF). The actual size of the buffers for currently open sockets may be inspected using the ss command (part of the iproute package), which can also provide a bunch more info on sockets like congestion control parameter etc. E.g. To list the currently open TCP (t option) sockets and associated memory (m) information:

ss -m --info
ss -tm

State       Recv-Q Send-Q        Local Address:Port        Peer Address:Port
ESTAB       0      0             192.168.56.102:ssh        192.168.56.1:56328
skmem:(r0,rb369280,t0,tb87040,f0,w0,o0,bl0,d0)

# Here's a brief explanation of skmem (socket memory) - for more info you'll need to look at the kernel sources (e.g. sock.h):
r:sk_rmem_alloc
rb:sk_rcvbuf          # current receive buffer size
t:sk_wmem_alloc
tb:sk_sndbuf          # current transmit buffer size
f:sk_forward_alloc
w:sk_wmem_queued      # persistent transmit queue size
o:sk_omem_alloc
bl:sk_backlog
d:sk_drops

# There is also /proc/sys/net/core/rmem_default for recv and /proc/sys/net/core/wmem_default for send
cat /proc/sys/net/core/rmem_default
cat /proc/sys/net/core/wmem_default

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
#
# aggregate size limitations for all connections, measured in pages; these values
# are for 4KB pages (getconf PAGESIZE)
 
/sbin/sysctl -w net.ipv4.tcp_mem='   65536   131072   262144'
 
# limit on receive space bytes per-connection; overridable by SO_RCVBUF; still
# goverened by core.rmem_max
 
/sbin/sysctl -w net.ipv4.tcp_rmem=' 262144  4194304  8388608'
 
# limit on send space bytes per-connection; overridable by SO_SNDBUF; still
# goverered by core.wmem_max
 
/sbin/sysctl -w net.ipv4.tcp_wmem='  65536  1048576  2097152'
 
# absolute limit on socket receive space bytes per-connection; cannot be
# overriden programatically
 
/sbin/sysctl -w net.core.rmem_max=16777216
 
# absolute limit on socket send space bytes per-connection; cannot be overriden;
# cannot be overriden programatically
 
/sbin/sysctl -w net.core.wmem_max=16777216

 

posted @   张同光  阅读(591)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示