Building a Memory Allocator from Scratch in C
kiirecodes
28 points
4 comments
May 11, 2026
Related Discussions
Found 5 related stories in 90.7ms across 8,303 title embeddings via pgvector HNSW
- mimalloc: A new, high-performance, scalable memory allocator for the modern era matt_d · 22 pts · May 14, 2026 · 58% similar
- Why is the first C++ (m)allocation always 72 KB? joelsiks · 122 pts · March 01, 2026 · 57% similar
- How do I deal with memory leaks? (2022) theanonymousone · 81 pts · May 08, 2026 · 54% similar
- Linux Internals: How /proc/self/mem writes to unwritable memory (2021) medbar · 59 pts · March 08, 2026 · 51% similar
- Show HN: CodeYam Memory – comprehensive memory management for Claude Code nadis · 16 pts · March 04, 2026 · 50% similar
Discussion Highlights (4 comments)
msarnoff
This was a class assignment in the 15-213 class at Carnegie Mellon. The staff had set up a test suite and an online leaderboard to rank the speed of each student's malloc implementation. I figured out that the test cases allocated a disproportionate amount of X-byte blocks. I was able to get to the top by hardcoding a specific freelist just for X-byte blocks. Learned a lesson about easily it is to game a benchmark :)
tkinom
Implemented my own specialized memory allocator 26+ yrs ago. (Y2K timeframe) Probably older than the most of the CMU students :-( Use pre-allocated pools with array of indexes, free/allocation idx for alloc and free. Con: Fixed pool size and fixed amount of memory can be allocated per pool. Pro: constant cost operations per alloc/free via Atomic inc/dec of idx - no linklist tranversing ; Can be alloc in kernel space and free in user space (linux/QNX) and in multiple user processes when memory pools are in shmem; Run very will in SMP environment without any locks - all memory contentions were handled with atomic +/- alloc/free idx. Same source code run in QNX, vxworks and linux (kernel and user space) at that time.
ethancedwards8
We also did this at Harvard in CS 61, which is the Systems Programming and Machine Organization class currently taught by Eddie Kohler (and has been for a while). It's a good exercise :)
tnelsond4
I was trying to get my c wasm module down in size and emmalloc is pretty small, but it requires a lot of js glue to make it work, but using a small allocator like walloc requires no glue and it's insanely small. I got my module down from 27kb to 17kb. I'm gonna read this article and try making my own allocator next.