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
- No
malloc/free— the compiler inserts all allocation and deallocation - No garbage collector pauses — regions are freed in O(1)
- No borrow checker annotations — only mutable borrows are checked
- Weak references are available to break reference cycles when using heap objects
The Region Hierarchy
| Region | Lifetime |
|---|---|
R_global | Entire program |
R_module | Module-level variables |
R_fn(name) | Function body |
R_block(id) | Lexical block |
R_loop(id) | One loop iteration |
Comparison with Other Languages
| Language | Model | Overhead |
|---|---|---|
| Go/Java/C# | Tracing GC | STW pauses, heap overhead |
| Rust | Ownership + borrow checker | Annotation burden, learning curve |
| C/C++ | Manual malloc/free | Use-after-free, leaks |
| Nyx | Region inference | Zero annotations, O(1) dealloc |