Memory Management

Key insight: Nyx uses region-based ownership — no garbage collector, no manual free(), no Rust-style borrow checker annotations. The compiler handles it for you.

How Region-Based Memory Works

Objects are created in regions (like arenas). When a region goes out of scope, all objects inside it are freed instantly — O(1) time.

fn process() {
    let user = User{ name: "Ada" }  // allocated in function's arena
    print(user.name)
    // user is automatically freed here — no ref counting needed
}

When Objects Escape

If an object needs to outlive its creation scope, the compiler automatically moves it or uses reference counting:

fn create_user() -> User {
    let user = User{ name: "Ada" }  // created in function arena
    return user  // compiler inserts a move to caller's region
}

fn share(data: String) {
    // string may be reference-counted on the heap if it escapes
}

No Manual Memory Management

The Region Hierarchy

RegionLifetime
R_globalEntire program
R_moduleModule-level variables
R_fn(name)Function body
R_block(id)Lexical block
R_loop(id)One loop iteration

Comparison with Other Languages

LanguageModelOverhead
Go/Java/C#Tracing GCSTW pauses, heap overhead
RustOwnership + borrow checkerAnnotation burden, learning curve
C/C++Manual malloc/freeUse-after-free, leaks
NyxRegion inferenceZero annotations, O(1) dealloc