Newcomer: Getting into Allocators

Hi folks, I’m used to languages with garbage collectors, so I haven’t had much experience working directly with memory allocation like Zig requires with allocators. I’d like to understand more about how allocators work, such as why there are so many different kinds, and how/when to choose one or another.

Can anyone recommend some good resources or content that could help me build a clearer understanding of allocators?

Thank you folks!

1 Like

Start with reading this:
https://ziglang.org/documentation/0.14.0/#Choosing-an-Allocator

Here are some more resources:

5 Likes

The zig doc section that @Sze linked needs updating.

While GeneralPurposeAllocator still exists, it’s now an alias to DebugAllocator and has had some internal changes as it’s now only meant for debug builds, for release builds there is smp_allocator which is thread safe and about as fast as glibc malloc, which is very impressive. Everything before GeneralPurposeAllocator is still valid

6 Likes

If you prefer video content. This is old but good intro to what allocator even is https://youtu.be/vHWiDx_l4V0

1 Like

I’ve found this blog post very helpful to get a grasp of how many allocator implementations out there work at a basic level. It introduces a lot of terms and concepts that you will encounter quite frequently when dealing with allocators. It’s not specific to Zig though.

2 Likes

I also strongly recommend studying the source code of SmpAllocator:

This is very short (if not very simple) allocator suitable for many workloads, and one of Zig programming gems.

(though, be aware that this allocator doesn’t handle some pathological workloads correctly)

1 Like

The word ‘correctly’ here makes me nervous.

Does it mean “becomes very slow / wastes a lot of memory”, or does it mean “leaves the heap in an invalid state”?

1 Like

See add mpsc example by matklad · Pull Request #1 · andrewrk/CarmensPlayground · GitHub, it can allocate unbounded amount of memory over time for a program with O(1) working set.

3 Likes