Overview
Zig provides several heap allocators optimized for different use cases. These allocators implement the standardAllocator interface and manage memory from the operating system.
DebugAllocator (GeneralPurposeAllocator)
A safe allocator designed for development and debugging. Detects memory leaks, double frees, and use-after-free bugs.GeneralPurposeAllocator is deprecated and renamed to DebugAllocator.Features
- Memory leak detection with stack traces
- Double-free detection with allocation and free stack traces
- Use-after-free protection (never reuses addresses)
- Alignment validation
- Configurable memory limits
- Thread-safe (configurable)
- Cross-platform
Basic Usage
Configuration
Methods
init
allocator
Allocator interface.
deinit
.okif no leaks detected.leakif memory leaks were found
detectLeaks
Number of leaks detected.
Memory Layout
DebugAllocator divides allocations into two categories: Small allocations (< page_size / 2):- Grouped into size-class buckets (powers of 2)
- Stored in fixed-size pages
- Fast allocation with O(1) complexity
- Includes metadata for debugging
- Allocated directly from backing allocator
- Stored in a hash map
- Supports efficient resize/remap operations
Performance Characteristics
Use DebugAllocator in Debug builds and switch to faster allocators in Release builds.PageAllocator
Direct syscall-based allocator that requests memory from the OS.Usage
Characteristics
- Thread-safe: Can be used from multiple threads
- Slow: Makes a syscall for every allocation
- Aligned: All allocations are page-aligned (typically 4KB)
- Zero-initialized: Memory is zeroed by the OS
- Direct mapping: No internal fragmentation
Platform Support
- Linux/Unix: Uses
mmap/munmap - Windows: Uses
VirtualAlloc/VirtualFree - WebAssembly: Uses
memory.grow - Plan 9: Uses
sbrk
C Allocator
Wraps C’smalloc, free, and realloc.
c_allocator
Allocator interface with alignment support.
Example:
raw_c_allocator
malloc/free wrapper. Alignment limited to @alignOf(std.c.max_align_t).
Use case: Backing allocator for ArenaAllocator.
Characteristics
- Fast: Optimized C implementation
- Thread-safe: Platform-dependent
- Platform-specific: Behavior varies by libc
- Requires libc: Only available when
builtin.link_libc == true
FixedBufferAllocator
Allocates from a fixed-size buffer. Fast but limited.Usage
Methods
init
reset
Characteristics
- Fast: Bump allocator (O(1) allocation)
- No free: Individual frees do nothing
- No growth: Returns
OutOfMemorywhen buffer full - Not thread-safe
WasmAllocator
Optimized for WebAssembly targets.Characteristics
- WebAssembly-specific: Uses
memory.growinstruction - Small and fast: Optimized for code size
- Page-aligned: 64KB page size
- Future default: Will be used by DebugAllocator in ReleaseSmall mode for wasm
ThreadSafeAllocator
Wraps any allocator to make it thread-safe.When to Use
- Wrapping non-thread-safe allocators for concurrent access
- Adding mutex protection to custom allocators
Many allocators (PageAllocator, c_allocator, DebugAllocator) are already thread-safe.
StackFallbackAllocator
Tries stack buffer first, falls back to heap allocator.Characteristics
- Hybrid: Fast for small allocations, flexible for large ones
- Automatic: Transparently switches between stack and heap
- One-time use: Call
.get()only once
Choosing an Allocator
Development/Testing
Development/Testing
DebugAllocator - Catches bugs with detailed diagnostics
Short-lived programs
Short-lived programs
page_allocator - Simple, no cleanup needed
High performance
High performance
ArenaAllocator (see next section) or c_allocator
Embedded/constrained
Embedded/constrained
FixedBufferAllocator - No heap, predictable
WebAssembly
WebAssembly
wasm_allocator - Optimized for size and WASM semantics
See Also
- Allocator Interface - Core allocation API
- ArenaAllocator - Bulk allocation pattern