Memory Management

Zith's memory model is built on Node Resource Analysis (NRA), a compile-time ownership tracker. This page covers allocation strategies, scenes, and runtime memory patterns. For the ownership keywords and NRA rules, see Memory Model — NRA.

Scenes (Memory Regions)

A Scene is a memory region (arena / zone allocator). Entering a new scene replaces the previous one — resetting memory rather than stacking. A scene may specify a custom Allocator.

// Default allocator
scene GameLevel {
    let terrain  = @allocate(Terrain, levelData);
    let entities = @allocate([_]Entity, entityList);
    runLevel(terrain, entities);
    // all memory freed here
}

// Custom bump allocator
scene GameFrame: BumpAllocator {
    let scratch = @allocate([4096]u8);
    renderFrame(scratch);
}

Scene Organisation

scene MainMenu {
    entity MenuButton { /* ... */ }
    entity TextDisplay { /* ... */ }
}

scene GameLevel {
    entity Player { /* ... */ }
    entity Enemy { /* ... */ }
    entity Item { /* ... */ }
}

// Transition between scenes
transition(MainMenu, GameLevel);   // old scene dies, new scene allocates

Allocation

Use the @allocate intrinsic to allocate within the current scene:

let ptr = @allocate(T, initialiser);
let arr = @allocate([_]T, [val1, val2, val3]);

NRA & Memory Origins

NRA tracks the origin of every memory node:

Origin Description
literal Compile-time constant in read-only memory.
stack Local variable on the call stack.
allocator Heap-allocated via scene or @allocate.
view Non-owning reference; origin follows the source.

Origin tracking enables zero-cost coercions — for example, a string literal (literal origin) can be used as []char without allocation.

See also: Spec: Memory Model (NRA)


Next: Error Handling