The message in dmesg reads like a joke when free -h shows eleven gigabytes available:
| |
No process was killed. No container restarted. Instead, throughput to one service collapsed, ss showed connections stuck in SYN-SENT and CLOSE-WAIT, log shipping fell behind, and every dashboard blamed the application. The kernel was not out of memory at all — it had exhausted a per-protocol, page-counted accounting limit that has almost nothing to do with how much RAM the machine has left.
That limit is net.ipv4.tcp_mem, and the way it is set, counted and confused with net.core.rmem_max is one of the most reliable sources of multi-hour outages in self-hosted infrastructure.
TL;DR — What To Do When Sockets Get Refused
tcp_memis measured in 4 KB pages, not bytes, and it is sum across every TCP socket. On this 27.7 GB host the default is330804 441075 661608pages — 1.29 GB, 1.72 GB and 2.58 GB respectively, i.e. roughly 4.7%, 6.2% and 9.3% of RAM.- The third number is a hard wall. Above it, the kernel refuses to grow socket buffers and logs
TCP: out of memory -- consider tuning tcp_mem. Free pages elsewhere are irrelevant; accounting is against the limit, not againstMemAvailable. - The usual culprit is per-socket buffer autotuning multiplied by socket count, not a single fat connection. A thousand sockets each allowed a 2.6 MB send buffer is 2.6 GB of accounted memory — more than the default ceiling.
- Fix the leak before the number. Orphaned sockets stuck in
CLOSE-WAIT/FIN-WAIT-2, or an application that never reads its receive queue, will refill any ceiling you set. - Do not touch
net.core.rmem_maxexpecting to changetcp_membehaviour. They are different layers:rmem_maxis a per-socket ceiling chosen by the application,tcp_memis a global protocol-wide budget chosen by the kernel.
Reading the Three Watermarks
| |
The kernel computes tcp_mem at boot from a fraction of physical memory, then keeps it fixed. Converting the three values into something a human can reason about:
| Watermark | Pages | Bytes | ≈ Size | What crossing it does |
|---|---|---|---|---|
| Low | 330,804 | 1,354,973,184 | 1.29 GB | Below this the kernel does not care; buffers are free to grow |
| Pressure | 441,075 | 1,806,643,200 | 1.72 GB | Enters memory pressure mode: buffer autotuning stops growing, receive queues get pruned defensively |
| Max | 661,608 | 2,709,946,368 | 2.58 GB | Hard refusal. No new allocations; TCP: out of memory is logged; new connections may fail |
Three properties follow from that table and they explain nearly every confusing symptom:
- Crossing “pressure” degrades silently. Autotuning freezes at the current window size, so throughput on long-fat-path connections quietly plateaus. Nothing is logged. This is the stage where you notice “the transfer is stuck at 40 Mbit” and find nothing in the logs.
- Crossing “max” fails loudly but late. By the time you see the message, accepted sockets are already being starved.
- The budget is shared by every socket on the host. One noisy neighbour — a backup job with 200 parallel streams, a metrics agent fanning out to 5,000 endpoints — spends the budget for every other service too.
The Real Numbers From This Host
Right now this machine is nearly idle, which makes the accounting visible:
| |
sockstat’s mem field is in pages, and it is the number compared against tcp_mem. Zero pages means nothing is queued. Note that during an incident this same field is the fastest possible signal: if mem is within a few percent of the low watermark, you are already in pressure mode.
Per-socket detail comes from ss -m, which exposes the kernel’s skmem accounting:
| |
| Field | Meaning | Why you care |
|---|---|---|
r / t | Bytes currently queued in receive / transmit | The only fields that consume the tcp_mem budget |
rb / tb | Allocated receive / transmit buffer size | This is the accounting charge, even when empty |
f | Socket “forward” allocation (kernel overhead) | Adds up across thousands of sockets |
w | Bytes waiting for the socket lock | Non-zero = contention, often a single hot listener |
o | Out-of-order queue bytes | Non-zero with TCPOFOQueue climbing = packet reordering or loss |
d | Drops on this socket | Rising d with Recv-Q pinned = application not reading |
That second socket is the whole story in one line: tb2626560 — a 2.6 MB transmit buffer allocated for an HTTPS connection. It is not in use (t0), but it is charged. Multiply by the number of concurrent connections and you get the budget.
Here is the arithmetic that catches people: 1,024 sockets × 2.6 MB = 2.66 GB, which exceeds this host’s 2.58 GB ceiling — while MemAvailable reads 11 GB. The kernel will refuse to grow buffers, log TCP: out of memory, and the machine will look completely healthy in every memory graph.
For comparison, the same host’s cumulative counters tell you whether it has actually happened:
| |
TCPMemoryPressures = 0 means the global wall was never hit here. PruneCalled = 1514 and TCPRcvCollapsed = 26898 mean the per-socket pruning path ran — the receive queue was collapsed because the application was not draining it fast enough. These are two different problems with similar names, and conflating them sends you tuning the wrong knob for days:
| Counter | Raised by | Fix |
|---|---|---|
TcpExtTCPMemoryPressures | Host-wide TCP budget crossed | Raise tcp_mem, reduce socket count or per-socket buffers |
TcpExtPruneCalled | One socket’s receive queue too full | Make the application read faster; check Recv-Q in ss |
TcpExtTCPAbortOnMemory | Socket killed for exceeding its own limit | Look for SO_RCVBUF misuse or an unbounded queue |
TcpExtTCPOFOQueue | Reordering / loss on the path | Nothing to tune on the host — see the packet-loss path |
The Four Real Causes of tcp_mem Pressure
1. Orphaned sockets. When an application closes a socket without reading the remaining data, or a proxy dies mid-transfer, the socket lingers in FIN-WAIT-2 or CLOSE-WAIT holding its buffers. net.ipv4.tcp_max_orphans = 131072 on this host means up to 131,072 orphans are tolerated — that is a lot of leaked memory before the kernel intervenes. ss -tan state close-wait is the single highest-value command in this entire article; a CLOSE-WAIT count that only grows is a leak, and no sysctl will save you.
2. Buffer autotuning with many concurrent connections. net.ipv4.tcp_wmem = 4096 16384 4194304 lets any single socket grow its send buffer to 4 MB, and tcp_rmem allows 32 MB of receive buffer. Both are per-socket and both count against tcp_mem. A service that keeps 500 connections to a slow upstream can legitimately claim gigabytes.
3. Receive-side backpressure. A consumer that cannot keep up causes the receive queue to fill, the window to shrink to zero, and the queue to be collapsed — all of which keep memory charged to the socket for as long as the connection lives.
4. Container and namespace multiplication. /proc/net/sockstat is per network namespace. A host with forty containers, each in its own netns, has forty TCP budgets that the host-level sysctl does not describe. If a container refuses connections, look inside its namespace:
| |
391,204 pages is 1.53 GB inside a container whose limit may still be the host’s default.
Tuning It Without Making Things Worse
The correct order is: prove the cause, fix the app, then raise the ceiling. Here is the drop-in for the last step:
| |
Those tcp_mem values are 1 GB / 2 GB / 4 GB — a 55% increase over the default ceiling, enough for roughly 1,500 sockets at the new 8 MB send-buffer cap. Verify:
| |
Two rules keep this safe. Never set tcp_mem max above ~20% of RAM — a TCP budget that large lets the protocol stack starve the page cache before the OOM killer is even a factor. And never raise tcp_mem without also capping tcp_rmem/tcp_wmem, or you have simply moved the wall further out while leaving the per-socket growth unbounded.
Decision Matrix
| Symptom | Most likely cause | Action |
|---|---|---|
TCP: out of memory in dmesg, MemAvailable high | Global TCP budget exhausted | Raise tcp_mem and cap tcp_rmem/tcp_wmem |
| Throughput plateaus, nothing in logs | Pressure mode — autotuning frozen | Check sockstat mem vs the low watermark |
CLOSE-WAIT count climbing steadily | Application or proxy not closing sockets | Fix the app; tcp_max_orphans only delays it |
Recv-Q pinned on one socket | Consumer too slow | Scale the consumer; PruneCalled will confirm it |
| Connections refused inside one container only | Per-netns budget | nsenter into the netns and inspect sockstat |
SYN-SENT piling up, few established | Not tcp_mem at all | Check backlog and filtering — see the queue guide below |
Pitfalls
tcp_memis in pages, everywhere, always. A value of661608is 2.58 GB on 4 KB pages, not 661 KB. Reading it as bytes leads to “my limit is 646 KB, that can’t be right” and an hour of confusion.net.core.rmem_maxdoes not gatetcp_rmem’s third value. They interact only when an application callssetsockopt(SO_RCVBUF): the kernel clamps tormem_max. Autotuning usestcp_rmemregardless, which is why raisingrmem_maxalone changes nothing.- Lowering
tcp_memon a busy host causes an immediate incident. The kernel does not shrink existing buffers to fit; it just refuses the next allocation. Change it in a maintenance window, not during a slowdown. sockstatis per namespace, andmemis not a total. Sum across namespaces if you want the host-wide figure —for ns in /var/run/netns/*; do ip netns exec ... doneor read each container’ssockstat.- IPv6 has its own accounting.
TCP6insockstat6is counted separately, and a dual-stack service can exhaust one while the other looks fine. TCPOFOQueueclimbing is not a memory problem. It is packet reordering or loss; tuningtcp_memwill not move it, but the packet loss diagnosis walkthrough will.tcp_notsent_lowatinteracts with send-buffer pressure. This host has it at4294967295(effectively “queue everything”), which maximises the chance a slow peer keeps a full send buffer charged. Setting a few hundred kilobytes is a cheap win for proxies and log shippers.
If your symptom is not memory pressure but connection acceptance — SYN retries, ListenDrops, accept queue overflow — the listen backlog and accept queue guide covers that failure mode, and if you are running out of ephemeral ports instead, read the TIME_WAIT port exhaustion diagnosis. Both produce failures that look like tcp_mem pressure when read from a dashboard, and neither is fixed by sysctl tuning.
FAQ
Why does the kernel say “out of memory” when free -h shows plenty?
Because tcp_mem is not a measure of physical memory availability. It is a fixed budget the kernel reserves for TCP socket buffers, computed at boot as a fraction of RAM. Accounting is against that budget, so a host with 11 GB available can still refuse to grow a socket buffer.
What are the three numbers in tcp_mem?
Low, pressure and max, in 4 KB pages. Below low nothing happens. Above pressure the kernel stops autotuning buffer growth and prunes defensively while logging nothing. Above max it refuses new allocations and logs TCP: out of memory -- consider tuning tcp_mem.
Is tcp_mem shared across containers?
No — it is per network namespace, and it is initialised from the host value when the namespace is created. A container can exhaust its own budget while the host is idle, so always inspect /proc/net/sockstat inside the namespace.
Should I raise tcp_mem or tcp_rmem?
Both, in that order of importance. tcp_mem is the wall you are hitting; tcp_rmem/tcp_wmem determine how fast any single socket consumes it. Raising only the ceiling lets one socket take a larger share of a larger budget.
How do I see which sockets are consuming the budget right now?
ss -tmi prints per-socket skmem accounting including allocated buffer sizes. Sort by the tb/rb values to find the top consumers, then check whether those bytes are actually queued (t/r) or just allocated and idle.
Does raising tcp_mem to a huge value fix slow transfers?
No. Slow transfers with no logs are usually the pressure watermark freezing autotuning, or receive-side backpressure from a slow consumer. A larger budget helps only if TCPMemoryPressures is actually incrementing.
What is a safe upper bound for tcp_mem max?
Keep it under roughly 20% of physical RAM. Beyond that the TCP stack can consume memory the page cache and other subsystems need, and you trade a clean out of memory log line for system-wide reclaim stalls.
💰 想测试你的市场判断力?我用 Polymarket 做预测市场交易——这是全球最大的预测市场平台,从大选结果到技术监管时间线,什么都可以押注。和赌博不同,这是真正的信息市场:你懂的信息越多,胜率越高。我靠预测技术相关事件的走向已经赚了不少。用我的邀请链接注册:Polymarket.com