Skip to content

Memory Management

Wyn uses Automatic Reference Counting (ARC). Every heap-allocated value (strings, arrays, HashMaps) has a reference count. When the count reaches zero, the memory is freed immediately.

How It Works

wyn
fn main() -> int {
    var s = "hello"       // RC = 1
    var t = s             // RC = 2 (shared reference)
    // t goes out of scope → RC = 1
    // s goes out of scope → RC = 0 → freed
    return 0
}

You don't manage memory manually. The compiler inserts retain/release calls automatically.

Scope-Based Cleanup

Strings, arrays, and HashMaps are freed at block exit:

wyn
fn process() -> int {
    var items = "a,b,c".split(",")   // Array allocated
    var upper = "hello".upper()       // String allocated
    var m = {"key": "value"}          // HashMap allocated
    // ... use them ...
    return 0
}   // items, upper, m all freed here

String Optimizations

Wyn's RC header caches string length and capacity:

  • .len() is O(1) — reads cached length, no strlen scan
  • String append (s = s + "x") is O(1) amortized — reuses buffer when refcount is 1
  • Method chains (s.upper().trim()) release intermediates automatically

Zero Leaks

v1.10 has zero known memory leaks. All patterns are verified with AddressSanitizer:

  • String concat temporaries
  • Function argument temporaries
  • Method chain intermediates
  • Unused return values
  • Array and HashMap scope cleanup

Thread Safety

Reference counting uses atomic operations. Strings can be safely shared across spawn boundaries. The RC system uses memory_order_acq_rel for release and memory_order_relaxed for retain.

ARC vs Garbage Collection

Most compiled languages use one of three memory strategies:

  1. Manual management (C, Zig) — you call malloc/free. Maximum control, maximum risk of leaks and use-after-free.
  2. Garbage collection (Go, Java, C#) — a background process finds and frees unreachable objects. Convenient, but causes unpredictable pauses (Go's GC pauses are typically <1ms, Java's can be 10-100ms+).
  3. Reference counting (Wyn, Swift, Objective-C) — each object tracks how many references point to it. When the count hits zero, memory is freed immediately. Deterministic, no pauses, but adds a small per-operation overhead.

Wyn chose ARC because it gives you deterministic cleanup (important for file handles, database connections, and network sockets) without the complexity of manual memory management. The ~2-5% overhead from atomic reference counting is a good tradeoff for application-level code.

How Compilation Affects Memory

Wyn is a compiled language — your source code is translated to C, then to a native binary. This means:

  • No interpreter overhead (Python's interpreter adds ~10MB RSS just to start)
  • No VM overhead (Java's JVM adds ~50-100MB RSS)
  • Memory layout is determined at compile time, not runtime
  • The ARC system is compiled into your binary as inline retain/release calls

A Wyn hello world uses ~1MB RSS. The same program in Python uses ~10MB, in Java ~50MB, in Node.js ~30MB.

See Also

MIT License — v1.11