liveBook · Manning

Stack memory

As explained in High Performance Unity Game Development

A region of runtime memory typically used for function call frames and short-lived local values.

Suppose a game checks which enemies are close enough to attack. The count, loop index, and distance calculation are needed only while the check runs. Stack memory is suited to such short-lived function data: it is limited in size, follows a last-in, first-out model, and its space can be reclaimed when a function returns. section 2.2.1

Listing 2.2 A function to count enemies in a specific range
public int CountEnemiesInRange(
    EnemyData enemyData,
    Vector2 playerPosition,
    float range,
    int numEnemies)
{
    int count = 0;
    for (int i = 0; i < numEnemies; i++)
    {
        float distance = Vector2.Distance(enemyData.Position[i], playerPosition);
        if (distance <= range)
        {
            count++;
        }
    }
    return count;
}

The enemy-range function makes the distinction concrete: its temporary working values belong to the call, while its enemy-data parameter is a reference to data held elsewhere. section 2.2.1

Think of each call as using a stack frame for its temporary data. A call made inside that function finishes before the outer call does, so their lifetimes fit the last-in, first-out pattern. This is a useful model, not a promise about physical placement: a compiler or runtime may keep values in registers, inline calls, or arrange frame contents differently. section 2.2.1

Before changing an allocation in a per-frame Unity path, distinguish the value from what it refers to:

  • A local array variable may be part of a stack frame, but an ordinary C# array’s contents are allocated on the heap. Setting the variable to null does not immediately make that heap space reusable. section 3.2.1
  • A small, temporary C# struct can be appropriate for values used together; passing a struct by value copies it. A struct stored inside a heap object is not thereby stored on the stack. section 2.7 section 2.8
  • Keep genuinely temporary allocations short-lived where practical, but use the heap for long-lived game data and dynamically allocated arrays and strings. section 2.7 section 3.2.4

The payoff is simpler lifetime management and, for stack-resident data, contiguous storage that can help data locality. It is not a reason to move every game structure onto the stack: capacity and lifetime limit its use. Nor is an array used *like a stack* necessarily stack memory; an enemy-index array can implement last-in, first-out access while remaining an array. section 2.8 section 6.5.3

How High Performance Unity Game Development covers it

How deep it goes
Works through function frames and nested calls using a simplified model; leaves exact placement to runtime, compiler, architecture, and optimization settings.
Examples
Uses enemy-range functions, an array reference pointing to heap contents, and per-frame enemy display changes.
What it assumes
Assumes familiarity with C# functions, local variables, arrays, and structs; defines last-in, first-out stack behavior.
What you can do after
Evaluate/understand whether short-lived values fit stack lifetimes without mistaking a local array reference for stack-allocated array contents.
How it connects
Connects stack memory to heap allocation, data locality, C# structs, value copying, and garbage collection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.