Skip to content
← All posts
Why

Why Velt has no garbage collector#

Velt programs never stop to clean up. Memory is freed the moment the last reference to it goes away, and you never write an annotation to make that happen.

If you've run a Node service under load, you've probably seen latency spikes, and some of them were the garbage collector: the runtime stops your code, finds what's still reachable and frees the rest. Usually that's quick. But it happens when the runtime decides, not when you do.

Velt compiles TypeScript to a native executable, so it could choose differently. It does: there is no collector, and nothing ever pauses the program.

How memory gets freed instead#

The compiler works out who owns each value and when it changes. That gives every object one of two kinds of life:

Single ownerMovedNo count, no overhead. Freed at the end of its owner's scope.
SharedReference countedOne word in front of the object. Freed when the count reaches zero.
NeverPaused for a GCFreeing is deterministic: no collector, no stop-the-world.

You don't choose between them, and there's no mut, borrow or lifetime syntax to learn. Objects behave as in JavaScript: assigning or passing one gives you the same object. The compiler picks the cheapest kind that's correct.

Cleanup you can read off the page#

Because freeing is deterministic, cleanup runs at a point you can see. Give a class a [Symbol.dispose]() method and it runs when the last reference goes away:

temp.velt
class TempFile {
  path: string;

  constructor(path: string) {
    this.path = path;
  }

  [Symbol.dispose]() {
    console.log("removing", this.path);
  }
}

{
  const t = new TempFile("a.tmp");
  console.log("using", t.path);
}                               // prints "removing a.tmp" here
console.log("after");

In Node, that cleanup would wait for a finalizer at some unknown point later, if it ran at all. In Velt it runs on the closing brace, every time. TypeScript's using declarations work too.

Weak references, without waiting#

Caches and observers want to refer to an object without keeping it alive. WeakRef, WeakMap and WeakSet work as in JavaScript, with one difference you'll like: they let go the moment the last strong reference goes, not at the next garbage collection.

ts
class Item {
  name: string;

  constructor(name: string) {
    this.name = name;
  }
}

const sizes = new WeakMap<Item, number>();
let n: Item | null = new Item("a");
sizes.set(n, 42);
const ref = new WeakRef(n);

n = null;                      // the last strong reference goes...
console.log(ref.deref());      // ...and the WeakRef is empty right away: undefined

What it costs#

Nothing is free, so here's the honest trade-off.

Tracing GC (Node)Velt
PausesYes, when the runtime decidesNever
When memory is freedSome time laterWhen the last reference goes
Per-object costHeader plus heap headroomNone if single-owner; one word if shared
Weak referencesLet go at the next collectionLet go at once
Reference cyclesCollectedNot freed: break them, or hold one side weakly

Cycles are the classic weak spot of reference counting: two objects that point at each other, or an object holding a closure that captured it, keep each other alive. Today you break the cycle when you're done (replace the field), or hold the back-pointer in a WeakRef. Next on the roadmap are weak fields (parent: weak Node | null) and a compile-time warning when a type can form a cycle, still without a collector.

What it buys you#

The clearest result is memory. In our hello-world HTTP server benchmark under load (Apple M4, Node 24), Velt peaked at 20.7 MB against Node's 130 MB, taking the highest reading of the runs (benchmarks). And latency has no collector spikes, because nothing ever stops the program.

The details, including the order things are freed in and what using guarantees, are in the memory model. For a gentler start, read Memory without a garbage collector in the book.
Andreas Ekdahl · Questions or pushback? Open an issue on GitHubMore posts