Severe Memory Leak on MacOS #36533
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#36533
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.1770+5d7cf3f34
Steps to Reproduce, Observed Behavior, and Expected Behavior
Hello, I am on MacOS and noticed that there is a severe memory leak with Zig in general:
Command for all the following code:
zig run test.zigSimplest memory leak I could achieve:
Using the
std.heap.page_allocator"fixes" the issue.In this case the
smp_allocatorseems to fix the issue too.std.Io issues
Originally, I stumbled upon this issue while playing around with the
std.Iointerface.In the example below, even the
smp_allocatorhas a memory leak of around 0.2 MB per second:Using
page_allocatorhere fixes the issue too.I am unfortunately not that deep into low level memory management debugging yet to know what I could do to further debug this myself.
I booted up a Linux x86_64 VM and tried the code (the std.Io one) there and it seems to work fine there, so I guess this is a MacOS specific issue 🤔
My System
Tested zig versions:
0.16and0.17.0-dev.1767+63cfe88f0and0.17.0-dev.1770+5d7cf3f34Below an image what it looked like in Activity Monitor after a couple of seconds with the std.Io leak AND the debug allocator that I got from
std.process.Init.io:If you wanted to take a look further, based on what you’ve written and the logic in
start.zigit seems like you’re seeing the behavior ofstd.heap.SafeAllocator, which does not appear to be macOS-specific in any way.Just a guess, but for the first example note that the standard allocator
std.heap.SafeAllocatorcontains safety mechanisms in debug build modes (whichzig rundefaults to if no other-O [mode]is given), f.e. tracking to prevent double-frees.(I suspect that in particular the "Virtual Memory Size" in your screenshot being 416.65 GB could be an artifact of freed addresses being marked as "don't reuse" to the OS - but that's just a guess as well.)
Memory usage probably still shouldn't grow unbounded... not sure where a limit would currently be configured. (Maybe safety data would be released/reused once an actual
error.OutOfMemoryis encountered from the backing allocator? Though that might still be undesirable / too late on systems with overcommit?)I don't think this explains a leak in the
std.Ioexample though.The leaked memory (at least in the first example) comes from capturing the stack traces of each allocation(/deallocation?). There are two issues here. The first is the lack of
.deinitwhich I fixed in the PR. It makes the number of frees roughly match the number of allocations (minus some small constant).There is also a second issue and I don't know what is the best way to fix it.
lib/std/debug/SelfInfo/MachO.zigusesstd.debug.getDebugInfoAllocator()which returns a global arena allocator by default.MachO.zigcorrectly frees the memory, however, the arena only respectsfreeif the last allocation is freed, so the memory ends up never being freed. I didn't look why this doesn't happen on other architectures.You can uncomment the if statement at the beginning of
getDebugInfoAllocatorto see the function where the memory is allocated. If you makegetDebugInfoAllocatorreturn a different allocator (for example smp_allocator), the leak is fully fixed.