skip to content

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%

answer

  1. two symptoms, both lifetime bugs
  2. allocations per frame by call site
  3. snapshot at the same quiet point
  4. total climbs, or largest block shrinks
  5. level arena, persistent heap apart

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.

solid answer

~50 s

The hitch: a general-purpose allocator is usually quick, but its slow path, taking a lock other threads hold, searching or splitting free blocks or asking the operating system for pages, can cost far more, and a frame that hits it repeatedly spikes. Capture the hitch frame with allocation tracking, count allocations and bytes per frame by call site, and move them: temporaries to a frame allocator, spawned objects to pools, containers reserved to their peak size at load. The out-of-memory: take a snapshot at the same quiet point, say the menu after unloading, after each load. If the total by category climbs, something long-lived still references level objects, a leak. If the total returns but the largest free block shrinks, long-lived allocations made during the level are pinning holes between freed level data. Both fixes are a lifetime split: level data in a per-level arena, persistent data in its own heap, handles into level memory.

go deeper

for a junior

Recall that different data lives for different lengths of time and that engines give each lifetime its own allocator.

for a middle

Explain why a general heap allocation can be slow occasionally, and why a container that doubles its size mid-match causes a spike.

for a senior

Lay out the diagnosis: allocation counts per frame by call site, snapshots at one quiet point, total versus largest free block, and the move to level arenas and pools.

for a principal

Turn the fix into policy: allocation tagging, automated level-cycling soak tests and a ban on general-heap use mid-match, with the exceptions a lead reviews.

## Two symptoms, one root cause The scenario has two complaints that look unrelated: - **Mid-match hitches** traced to calls into the **general-purpose heap allocator**. - **Out-of-memory after several level loads**, although each level fits comfortably on its own. Both usually come from the same design gap: data with **very different lifetimes** shares one general heap. Frame temporaries, projectiles, level geometry and data that lasts the whole session all go through the same allocator, so it has to cope with the worst mix and pays for it in time (the hitch) and in space (memory that does not come back). The fix is to **allocate by lifetime**, but the diagnosis has to prove which lifetimes are mixed. ## Diagnosing the hitch 1. **Capture a hitching frame** with a profiler that records allocator calls, with call stacks or allocation tags, and compare it with a smooth frame. 2. **Count allocations and bytes per frame by call site.** A smooth mid-match frame should make few or no general-heap calls; a hitching frame often shows a burst, for example one per spawned projectile or one per growing container. 3. **Look for the allocator's slow path**: time spent waiting on the heap's lock (worker threads allocating at once), in searching and splitting free blocks, or in requesting fresh pages from the operating system, which may also zero them. 4. **Fix by lifetime**, call site by call site: - temporaries that die this frame go to a **per-frame linear allocator**; - spawned same-size objects go to **pools** reserved at load; - containers that grow mid-match are **reserved to their measured peak** at load, because a dynamic array that doubles when full copies everything at the worst moment. A development-build assert that fires on general-heap allocation during gameplay keeps the problem from returning. ## Diagnosing the creeping out-of-memory Take a **memory snapshot at the same quiet point** after each level load, for example the main menu after the previous level has unloaded, and compare snapshots across five or ten load cycles. | What the snapshots show | What it means | Where to look | |---|---|---| | Total used climbs every cycle | a **leak**: level-lifetime objects still referenced | caches, event listeners, registries holding level objects | | Total returns to baseline, largest free block shrinks | **fragmentation**: free memory exists but in pieces too small | long-lived allocations made during the level | | Both stable, yet the next load fails | the level's peak exceeds its budget | per-category peaks during the load itself | The fragmentation case is the one the scenario hints at: during a level, systems make a few **long-lived** allocations (a session statistic, a cached string, a save-game record) from the same heap as level data. When the level unloads, the level data is freed but those survivors stay scattered across the address range, so the next level's large contiguous allocations no longer fit even though total free memory is enough. The general theory of fragmentation is foundations material; the engine's job is to keep lifetimes apart so it cannot happen. ## The fix: allocate by lifetime | Lifetime | Allocator | Released | |---|---|---| | One frame | per-frame linear allocator | offset reset each frame | | Read one frame later | double-buffered frame allocator | every other frame | | Same-size, varying | pools | slot by slot, constant time | | Exactly one level | per-level arena | all at once on unload | | Whole session | persistent heap | rarely, at shutdown | The **per-level arena** is the decisive change for the out-of-memory: everything the level owns comes from one region that is released in a single operation, so nothing can be left scattered. Persistent systems allocate only from the persistent heap and refer into level memory through **handles**, never raw pointers, so an unload cannot leave them dangling and a stale reference fails cleanly. ## Making it stick - **Tag every allocation** with a category so snapshots and budgets report by owner. - **Soak-test level cycling** in automated runs: load and unload every level many times and compare snapshots, failing the run when the baseline drifts. - **Track the largest free block**, not just total free, in memory reports. - **Assert on general-heap use mid-match** in development builds, with an allowlist for rare, reviewed exceptions.

  • Why compare snapshots at the same quiet point instead of during gameplay?
    During gameplay, memory legitimately rises and falls with streaming, effects and spawns, so a difference proves nothing. At one fixed quiet point, such as the menu after unload, every level-lifetime allocation should be gone, so any growth between cycles is either a leak or the residue of fragmentation.
  • How do you tell a leak from fragmentation using only memory reports?
    A leak raises total used memory each cycle; the tagged categories show which owner grows. Fragmentation leaves total used at baseline while the largest free block shrinks, so a large request fails despite enough free memory. Reports that track only total free will miss the second case.

  • General heap allocations always take the same short time
  • Running out of memory with enough total free memory is impossible
  • Comparing snapshots taken mid-gameplay proves a leak
  • Letting a container double its capacity mid-match is harmless
  • Freeing every level object one by one fixes fragmentation