skip to content

Memory Management Strategies

Which allocator an engine uses for which lifetime: per-frame linear allocators, per-level arenas and pools, plus handles over raw pointers. Interviewers probe it: frame spikes are memory stories.

on this pageshow

explore

questions

6

Why does a game engine keep fixed-size objects such as projectiles in a preallocated pool instead of allocating each one from the general heap?

level: juniorimportance: must knowfreq 60%

answer

  1. same size, many births and deaths
  2. reserve every slot at load
  3. free list through unused slots
  4. constant-time acquire and release
  5. capacity and exhaustion policy

basics

~20 s

A pool reserves every slot up front, so spawning or despawning a projectile is a constant-time free-list pop or push: no general-heap call, no heap fragmentation from tiny short-lived blocks, and a fixed memory cost known in advance.

solid answer

~50 s

Projectiles, particles, decals and hit effects are created and destroyed hundreds of times a second, and every instance of one type is the same size. A pool allocates one block of `N` slots at level load and links the free slots into a free list, kept inside the unused slots or in a parallel index array. Acquiring a slot pops the head of that list and releasing pushes it back: both are constant time, with no search and no trip into the general-purpose allocator. Because any free slot fits any request, the pool cannot fragment, the general heap is not churned by thousands of tiny blocks, and the pool's memory is one fixed, visible line in the budget. Live objects also sit in one contiguous block, which helps a system that iterates over them. The price is choosing a capacity, choosing what happens when it runs out, and resetting every field when a slot is reused.

code

pseudocode · 22 lines
pseudocode
ProjectilePool
  slots:    array[CAPACITY] of Projectile
  nextFree: array[CAPACITY] of int
  freeHead: int

  init():
    for i in 0 .. CAPACITY - 2:
      nextFree[i] = i + 1
    nextFree[CAPACITY - 1] = NONE
    freeHead = 0

  acquire():
    if freeHead == NONE:
      return NONE              // exhaustion policy decides what happens next
    i = freeHead
    freeHead = nextFree[i]
    slots[i].reset()           // clear every field left by the previous user
    return i

  release(i):
    nextFree[i] = freeHead
    freeHead = i

go deeper

for a junior

Explain that a pool reserves same-size slots up front and hands them out and takes them back in constant time, and name projectiles or particles as the classic use.

for a middle

Walk through the free list: where the next-free links live, what acquire and release do, and why a same-size pool cannot fragment. Mention resetting slots on reuse.

for a senior

Discuss capacity from measured peaks, the exhaustion policy per object type, and how stale pointers into a pool are caught with generation-checked handles.

for a principal

Frame pools as one tool in an allocate-by-lifetime policy, and say how a team keeps pool sizes honest across platforms with telemetry rather than guesses.

## The problem a pool solves A shooter can fire, spawn and destroy hundreds of **projectiles** a second; a particle system does the same with sparks and smoke puffs. Each of these objects: - is **the same size** as every other object of its type, - lives for a short and unpredictable time (a bullet dies when it hits something, not at the end of a frame), - is created and destroyed **mid-match**, inside the frame budget. Sending each of those births and deaths to the **general-purpose heap allocator** is the slow, risky choice. A general allocator has to handle any size and any lifetime, so its worst-case path can search for a block, split or merge blocks, take a lock that other threads also want, or ask the operating system for more memory. Thousands of tiny blocks with scattered lifetimes also leave the general heap pocked with small holes. An **object pool** removes all of that for one type of object. ## How a pool works 1. **Reserve** one contiguous block of `N` slots, each the size of one object, when the level loads. 2. **Link** every slot into a **free list**: each free slot records the index of the next free slot, either inside the unused slot's own bytes or in a parallel array of indices. A `freeHead` index points at the first free slot. 3. **Acquire**: take the slot at `freeHead`, move `freeHead` to that slot's successor, reset the slot's fields and hand it out. 4. **Release**: write the current `freeHead` into the slot and make the slot the new `freeHead`. Steps 3 and 4 update a couple of indices each, plus the slot reset on acquire, so both are **constant time**, whatever the pool's size and however many objects are live. ## What the pool buys - **Predictable cost.** No search and no call into the general allocator during the match, so spawning a burst of fifty projectiles cannot hit an allocator slow path. - **No fragmentation inside the pool.** Every request is the same size, so any free slot fits it; a released slot is never a hole too small to reuse. - **A known memory figure.** The pool costs `N` times the slot size, decided at load, which fits neatly into a per-platform memory budget. - **Locality.** Live objects sit in one block, so a system updating every projectile walks nearby memory instead of chasing scattered heap addresses. - **Simplicity of ownership.** One system owns the pool, and a pool used from a single thread needs no lock (a pool shared between threads does need its own synchronisation). ## Capacity and exhaustion A pool is a promise: at most `N` live objects. The design question is what happens at `N + 1`. | Policy | Suits | Cost | |---|---|---| | Recycle the oldest live object | cosmetic effects such as decals and sparks | an old effect vanishes early | | Refuse the spawn | objects whose loss is invisible or harmless | the new object never appears | | Grow by a whole new chunk | rare peaks you cannot bound | memory rises, and a growth step mid-match costs time | Growing by **reallocating the whole array and copying it** is the policy to avoid: it moves every live object, so any pointer into the pool dangles, and the copy is a spike of its own. Capacity is set from measured peaks plus some headroom, not from a guess. ## The traps - **Stale state.** A reused slot still holds the previous object's data; a projectile that skips its reset can inherit the last one's target or velocity. - **Dangling references.** Another system that kept a raw pointer to a released projectile now points at whichever projectile reused the slot. Engines pair pools with handles that carry a generation count to catch this. - **Double release.** Releasing the same slot twice puts it on the free list twice, and two later spawns share one slot. A debug build can mark released slots and assert. - **Iterating only live slots.** A pool's array also holds free slots, so iteration needs a live flag or a dense list of live indices. ## Where pools sit among engine allocators Pools serve **same-size objects with varying lifetimes**. Data that lives exactly one frame belongs in a per-frame linear allocator, and data that lives exactly as long as the level belongs in a per-level arena. Choosing among them by lifetime, not by habit, is what keeps both hitches and memory growth away.

  • Why not just make the general-purpose allocator faster instead of adding pools?
    A general allocator has to serve every size and lifetime, so its worst case stays expensive even when the average is good: searching, splitting, locking, or fetching pages from the operating system. A pool knows the size and owns its block, so its worst case equals its average. Engines want a bounded worst case inside a frame budget more than a better average.
  • How would you size a projectile pool?
    Measure the peak number of live projectiles in the heaviest gameplay you can stage, add headroom, and pick the exhaustion policy for that object type. Log or count every time the pool hits its cap in development builds, so a too-small pool shows up as data rather than as missing bullets in a bug report.

A pool is a coat check with a fixed rack of numbered hooks: hanging or collecting a coat is instant, every hook fits every coat, and when all hooks are taken the attendant needs a rule for what happens next.

  • Pools exist only to save memory compared with the heap
  • A pool is just an array that grows by copying when full
  • A reused pool slot does not need resetting
  • Pools remove the need to track who still refers to an object
  • Releasing a slot returns its memory to the operating system
open as a page

How does a per-frame linear allocator in a game engine work, and when do you need a double-buffered version instead of a single buffer?

level: middleimportance: must knowfreq 55%

basics

~20 s

A per-frame linear allocator bumps an offset through one preallocated buffer and frees everything by resetting the offset once a frame. Data still read during the next frame needs two buffers used alternately, so a reset never destroys it.

open as a page

Why do game engines refer to entities and resources through handles with generation counts rather than raw pointers, and how is a stale handle caught?

level: middleimportance: should knowfreq 50%

basics

~20 s

A handle is a slot index plus a generation number. Freeing an object bumps its slot's generation, so resolving an old handle finds a mismatch and returns nothing instead of touching a dead object or the new one that reused the slot.

open as a page

In a game whose gameplay runs in a garbage-collected scripting language, why do per-frame allocations cause periodic hitches, and how are they removed?

level: middleimportance: should knowfreq 45%

basics

~20 s

Per-frame allocations become garbage, and the collector runs when enough piles up, stealing milliseconds from whichever frame it lands in. The fix is zero steady-state allocation in gameplay scripts: preallocate, pool, reuse buffers, and schedule collections at loading screens.

open as a page

A console game hitches mid-match on general-purpose heap allocations and runs out of memory after several level loads, though each level fits alone; how do you diagnose and fix it?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Treat both as lifetime bugs: track allocations per frame and move mid-match ones into pools, frame allocators or load-time reservations; snapshot memory at one quiet point after each level load, then give level data an arena freed at once.

open as a page

As the lead on a game shipping to several consoles, PC and dedicated servers, how do you set and enforce memory budgets per platform and per subsystem?

level: principalimportance: should knowfreq 38%

basics

~20 s

Budget from each platform's retail memory, split by category with named owners and a held-back reserve, and make it measurable: tag every allocation, record per-level peaks in automated runs, fail builds that exceed. Dedicated servers budget per match process.

open as a page