⭐⭐⭐⭐

Memory Allocation Strategies

Source: gingerbill.org | Author: gingerBill | Series Started: 2019
Memory C Systems Programming Optimization

Summary

A comprehensive series on memory allocation strategies for systems programmers. Challenges conventional wisdom about stack vs heap and provides a more nuanced understanding of virtual memory.

Key Insight: Beyond Stack vs Heap

Most programmers are taught a dualistic model: stack (automatic, fast) vs heap (manual, slow). This is wrong.

Reality: Modern OSes virtualize memory per-process. Memory is a linear address space that can be divided however you want.

The Monistic Model

  • All memory is virtual and linear
  • Stack is just one section of that address space
  • You can create your own "heaps" for different purposes
  • Understanding this opens up optimization opportunities

Allocation Categories

Size Known Size Unknown
Lifetime Known ~95% of allocations ~4% of allocations
Lifetime Unknown ~1% of allocations <1% of allocations

Most allocations have known size AND known lifetime! This means most allocations can be optimized heavily.

Topics Covered

  • Virtual memory fundamentals
  • Stack vs heap myth
  • Arena allocators
  • Pool allocators
  • Free-list allocators
  • Buddy allocator
  • Slab allocator
  • Stack allocators (linear)

Why This Matters

For game engines, real-time systems, and performance-critical code, understanding memory allocation is crucial. This series provides a foundation for writing custom allocators tailored to your use case.