skip to content

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%

answer

  1. average fine, spike every few seconds
  2. allocation rate drives collection rate
  3. hidden allocations in hot paths
  4. zero bytes per frame in steady state
  5. collect at a loading screen

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.

solid answer

~50 s

Gameplay scripts may own only a few milliseconds of a 33.3 ms frame at 30 Hz. If scripts allocate a few kilobytes every frame, through returned lists, closures, rebuilt strings or boxed values, that garbage accumulates until the collector runs. A collection lasting several milliseconds then lands inside one frame and blows it, so the average frame time looks fine while a hitch repeats every few seconds. Collections come more often the faster scripts allocate, so the cure is to stop allocating in steady state: allocate lists once and reuse them, pool short-lived objects, call query variants that fill a caller-owned buffer, rebuild display strings only when the value changes, and keep closures and boxing off hot paths. Then time what remains: use an incremental mode that slices collection across frames where the runtime offers one, and collect explicitly at loading screens.

go deeper

for a junior

Know that allocating in scripts every frame creates garbage and that the collector's pause can land in one frame as a visible hitch.

for a middle

Explain why allocation rate sets collection frequency, name the hidden sources such as closures, boxing, strings and returned lists, and give the reuse and pooling fixes.

for a senior

Show how you would measure bytes per frame by call site, set a zero steady-state target, and time remaining collections with incremental slices and loading-screen collections.

for a principal

Decide the scripting heap's budget and allocation-rate target per platform, and how automated runs catch a feature that starts allocating every frame.

## Why garbage turns into hitches Many games run their gameplay logic in a **managed scripting language**: a language whose runtime reclaims memory with a **garbage collector** instead of explicit frees. That suits designers and fast iteration, but it brings one memory rule that dominates performance: **every allocation made during a frame is future work for the collector**. The collector does not run on every allocation. Typically it runs when enough new memory has been allocated since the last collection, or when the heap fills. Its work then lands inside **one** frame. If that frame has a budget of a few milliseconds for scripts and the collection takes several, the frame overruns and the player sees a **hitch**. Because the trigger is the amount allocated, the hitch is **periodic**: average frame time looks healthy, while every few seconds one frame spikes. How a collector traces and frees is general memory-management theory; the engine's problem is keeping its pauses out of the frame. ## A worked example (scenario figures) Suppose gameplay scripts allocate **4 KB per frame** at **30 frames per second**: - garbage per second: 4 KB x 30 = **120 KB/s** - garbage per minute: 120 KB x 60 = **7,200 KB**, about 7 MB - if this runtime starts a collection after every 1,200 KB of new allocation, that is a collection every 1,200 / 120 = **10 seconds** In this model, halving the allocation rate halves the collection frequency, and reaching zero in steady state removes the periodic, allocation-driven collection. That is why teams budget **bytes allocated per frame**, not just milliseconds. ## Where hidden allocations come from | Source | Why it allocates | Usual fix | |---|---|---| | Query returning a new list | a fresh container per call | a variant that fills a caller-owned buffer | | Closure passed to sort or filter | captures variables in a new object | create the comparison once and reuse it | | String building for UI | every concatenation or format makes a new string | rebuild only when the value changes | | Boxing a value type | wraps a number or small record in a heap object | typed containers, avoid generic object slots | | Iterators and temporary arrays | per-call helper objects | index loops, reused arrays | Many of these never look like allocations in source code, which is why measuring beats reading. ## Removing them 1. **Measure per frame.** Use the runtime's allocation profiler to record bytes allocated per frame and per call site during normal play, not on a loading screen. 2. **Set the target** to zero bytes per frame in steady-state gameplay. Spawning a wave or opening a menu may allocate, but walking around must not. 3. **Preallocate and reuse.** Lists, arrays and string builders are created at load and cleared, not recreated, every frame. 4. **Pool short-lived objects.** Projectiles, temporary effects and event records come from pools instead of being created and dropped. 5. **Move hot code across the boundary when it pays.** A system that must chew through thousands of items per frame may belong in engine code, as long as the cost of crossing the script binding does not eat the gain. ## Timing what remains - **Incremental collection.** Some runtimes can split a collection into small time slices spread over many frames; give it a slice in each frame's budget instead of one big pause. - **Collect at safe points.** Trigger a full collection at loading screens, level transitions and pause menus, when a pause is invisible. - **Heap headroom.** A larger heap delays collections, but a collector that sweeps the whole heap pays more per collection as the heap grows; it is a trade, not a fix. - **Long sessions.** Test for an hour, not a minute: slow growth in the live heap makes each collection longer over time. ## The engine-side view The scripting layer's memory sits inside the same per-platform budget as everything else. A lead gives it a fixed heap size and an allocation-rate target, and the team watches both in automated runs, so a new feature that allocates every frame shows up as a regression the day it lands.

  • Why not call the collector manually at the end of every frame to keep each pause small?
    A full collection costs time roughly tied to live data and heap size, not just new garbage, so forcing one every frame pays that cost every frame and lowers the frame rate. Manual collection belongs at points where a pause is invisible, such as loading screens; inside gameplay, an incremental mode with a per-frame time slice is the tool.
  • How do you find an allocation that the source code does not show?
    Profile allocations per frame by call site during normal play. Hidden sources such as closures capturing variables, values boxed into objects, iterators and string formatting appear as allocation counts at lines that look allocation-free. Fix the top call sites, then re-measure until steady-state bytes per frame read zero.

  • Small allocations are free in a garbage-collected language
  • A good average frame time proves there is no collection problem
  • Forcing a full collection every frame keeps pauses short
  • A bigger heap always makes collection pauses shorter
  • Clearing a list each frame is the same as reusing it