# Newcomer: Getting into Allocators

**URL:** <https://ziggit.dev/t/newcomer-getting-into-allocators/9621>\
**Category:** Help\
**Tags:** language, memory-management\
**Created:** [April 14, 2025, 9:49pm UTC](https://ziggit.dev/t/newcomer-getting-into-allocators/9621 "2025-04-14T21:49:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bongolin](https://ziggit.dev/user_avatar/ziggit.dev/bongolin/32/2939_2.png) [@bongolin](https://ziggit.dev/u/bongolin)\
**Post date:** [April 14, 2025, 9:49pm UTC](https://ziggit.dev/t/newcomer-getting-into-allocators/9621/1 "2025-04-14T21:49:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [April 14, 2025, 10:08pm UTC](https://ziggit.dev/t/newcomer-getting-into-allocators/9621/2 "2025-04-14T22:08:39Z")

</div>

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

Here are some more resources:

- [Learning Zig - Heap Memory & Allocators](https://www.openmymind.net/learning_zig/heap_memory/)
- [3&nbsp; Memory and Allocators – Introduction to Zig](https://pedropark99.github.io/zig-book/Chapters/01-memory.html) from [GitHub - pedropark99/zig-book: An open, technical and introductory book for the Zig programming language 📚📖](https://github.com/pedropark99/zig-book)

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [April 15, 2025, 2:54am UTC](https://ziggit.dev/t/newcomer-getting-into-allocators/9621/3 "2025-04-15T02:54:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AndrewKraevskii](https://ziggit.dev/user_avatar/ziggit.dev/andrewkraevskii/32/2564_2.png) [@AndrewKraevskii](https://ziggit.dev/u/AndrewKraevskii)\
**Post date:** [April 15, 2025, 6:59pm UTC](https://ziggit.dev/t/newcomer-getting-into-allocators/9621/4 "2025-04-15T18:59:33Z")

</div>

If you prefer video content. This is old but good intro to what allocator even is [https://youtu.be/vHWiDx\_l4V0](https://youtu.be/vHWiDx_l4V0)

---

<div class="post-metadata">

**Author:** ![Justus2308](https://ziggit.dev/user_avatar/ziggit.dev/justus2308/32/5305_2.png) [@Justus2308](https://ziggit.dev/u/Justus2308)\
**Post date:** [April 15, 2025, 7:57pm UTC](https://ziggit.dev/t/newcomer-getting-into-allocators/9621/5 "2025-04-15T19:57:01Z")

</div>

I’ve found [this blog post](https://samwho.dev/memory-allocation/) 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.

---

<div class="post-metadata">

**Author:** ![matklad](https://ziggit.dev/user_avatar/ziggit.dev/matklad/32/2462_2.png) [@matklad](https://ziggit.dev/u/matklad)\
**Post date:** [April 15, 2025, 8:29pm UTC](https://ziggit.dev/t/newcomer-getting-into-allocators/9621/6 "2025-04-15T20:29:58Z")

</div>

I also strongly recommend studying the source code of SmpAllocator:

> <https://github.com/ziglang/zig/blob/6c598e8341c202a61dc24678f4450fd5af961b4e/lib/std/heap/SmpAllocator.zig>

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)

---

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [April 15, 2025, 10:44pm UTC](https://ziggit.dev/t/newcomer-getting-into-allocators/9621/7 "2025-04-15T22:44:36Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![matklad](https://ziggit.dev/user_avatar/ziggit.dev/matklad/32/2462_2.png) [@matklad](https://ziggit.dev/u/matklad)\
**Post date:** [April 15, 2025, 11:15pm UTC](https://ziggit.dev/t/newcomer-getting-into-allocators/9621/8 "2025-04-15T23:15:17Z")

</div>

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