SmpAllocator does not reuse freed small allocations when threads are created and destroyed frequently, causing unbounded RSS growth #36969
Labels
No labels
abi/f32
abi/ilp32
abi/sf
accepted
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/android
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
release notes
testing
zig cc
zig fmt
zig reduce
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
use case
No assignees
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ziglang/zig#36969
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Zig Version
0.17.0-dev.2294+71403f299
Steps to Reproduce, Observed Behavior, and Expected Behavior
Possible Related: #36533
Reproduce
run
zig run repro.zigon linux x86_64It output on my computer(8 core cpu, archlinux)
The Rss is increasing!!The
0.16.0alsoIf replace the
smp_allocatorwithpage_allocator,the rss is flat。After discovering this issue, I delved into the source code to investigate it further.
I change the
std/heap/SmpAllocator.zigon the structThreadto count(On zig0.16.0lib)and write a export function:
And then run the
repro.zig, new outputThe allocation thread is stuck on slot 1, performing “bump” operations (mapping 353 new slabs), while the released blocks are piling up in slots 2 and 3 and are virtually ignored. Meanwhile, large blocks (in the PageAllocator path) show a strict 1:1 ratio of maps to unmaps (90,000 each)—the issue occurs on small block
looks like #36294 fixes this
before
after
Another trigger for the same root cause, one that does not need thread churn: long-lived threads (e.g. a tokio multi-threaded runtime) where blocks are allocated on one thread and freed on another. We hit this with Rust binaries built via cargo-zigbuild for x86_64-unknown-linux-musl with Zig 0.16.0, where libc malloc is SmpAllocator (lib/c/malloc.zig).
▎
▎ Observed on a long-running service on a 2-vCPU host: bytes in use stayed flat over ~65 h while anonymous RSS grew linearly (~2–2.5 MB/h, not converging). Walking the allocator's global state showed essentially all of the growth sitting in free lists (mostly small size classes), piled up on slot 1 while slot 0 kept mapping new slabs. The same binary on a 1-vCPU host stays flat for 10+ days, and the same workload linked with jemalloc stays flat.
▎
▎ Mechanism, as far as I can read lib/std/heap/SmpAllocator.zig at 0.16.0:
▎
▎ 1. free (~L200–215) pushes the block onto the free list of whichever slot the freeing thread holds, never back to the slot it was allocated from.
▎ 2. In alloc, once the own free list is empty and the bump region is exhausted, it unlocks and tryLocks (index + 1) % cpu_count (~L160–172). With cpu_count == 2, if that slot is busy (often exactly the thread that is freeing into it), the loop wraps back to its own slot with search_count == 1, and max_alloc_search = 1 sends it straight to PageAllocator.map for a new 64 KiB slab (~L151–157), while the other slot's list holds plenty of free blocks of that class.
▎ 3. Small-class slabs are never unmapped and nothing rebalances between slots, so a steady producer/consumer pattern makes the consumer's free list grow without bound.
▎
▎ With cpu_count == 1, (index + 1) % 1 == 0: every thread shares one set of free lists, frees are always reused, and the growth disappears. That matches the 1-vCPU observation above.
▎
▎ Since SmpAllocator is the default libc malloc on multi-threaded non-wasm targets, this silently affects programs linked through zig cc, e.g. cargo-zigbuild users who picked up 0.16 (https://github.com/ZingerLittleBee/ServerBee/pull/180 pinned 0.15.2 for the same symptom). Routing frees back to the owning slot, as in #36294, looks like it would cover this pattern too.
Another data point for this issue, with a smaller repro: there's no thread churn, just two long-lived threads.
One thread allocates and the other frees. It also goes through libc
mallocwhen building C withzig ccfor static musl, becauselib/c/malloc.zigwrapsSmpAllocator. I hit this in a production daemon, an ADS-B receiver built-target aarch64-linux-musl -static. RSS grew without bound on Raspberry Pi 4/5 boxes, while a glibc build of the same code stayed at a 1–4 MB peak heap under heaptrack.Repro (
stdonly, no libc)At most 1024 × 400 B (~400 KiB) is ever live.
The C equivalent (same ring,
malloc/free, two pthreads) built withzig cc -target aarch64-linux-musl -static -O2 repro.c -lpthreadbehaves the same way.Results, Zig 0.17.0
aarch64 Fedora 44 VM with 4 vCPUs (Apple M-series host), RSS after 30 s:
taskset -c 0repro.zig(smp_allocator)repro.c,zig ccstatic muslrepro.c, glibcmallocrepro.zig+ #36294 applied to 0.17.0repro.c+ #36294 applied to 0.17.0The same
repro.zigon macOS 27 (aarch64, 14 cores) reaches 4 GB in 40 s.SmpAllocator.zigandlib/c/malloc.zigon master (0.18.0-dev.253+16d31ceed) are byte-identical to 0.17.0's, so master is affected too.Mechanism
This matches tokju's instrumentation:
freepushes onto the free list of whichever slot the freeing thread currently holds.max_alloc_search = 1) before mapping a fresh slab.taskset -c 0is flat.Impact
zig ccstatic musl binaries get this through plainmalloc, so C/C++ projects (and cargo-zigbuild users, as in BlackLuny's comment) are exposed without using any Zig code. The only workaround I've found short of replacingmallocis pinning the process to one CPU. #36294 fixes this repro for me.