^T is an address and the fields behind it are mutable, so an Atoll program
can build a cycle. Reference counting alone can never reclaim one: every block
in a cycle keeps another’s count above zero.
Atoll’s answer is to collect them. There is no @weak, no capture
annotation, no cycle diagnostic, and nothing you declare.
Nothing is refused, because nothing can be
A field that can point at its own type is exactly what a linked structure needs and exactly what a cycle needs. They are the same declaration and the same assignment:
a.next = b // a list
a.next = a // a cycleSeparating them at the type level is impossible — the types are identical. Separating them at each store would mean proving the target is not already reachable from the source, which is alias analysis and over-refuses in practice. So the compiler refuses nothing and asks you for nothing.
Trial deletion
A decrement that does not reach zero makes the block a candidate. A collection walks a candidate’s subgraph, subtracting the edges inside it from a shadow count. If a shadow reaches zero, every reference to that block came from inside the subgraph, and the subgraph is garbage.
Three properties follow, and they are why this rather than tracing:
- Work is proportional to garbage, not to the live heap.
- No root enumeration. A reference held by a parked frame is simply never subtracted, so the count stays positive and the block is kept. No stack maps, no safepoints, no marking phase.
- The edge enumeration it needs is the release walk the compiler already emits.
The real count is never touched — the subtraction happens in a shadow — because every collection mutator asks whether a buffer is uniquely owned before writing in place, and a deflated count would answer yes on a shared block. See Ownership for the mechanism.
When a collection runs
A collection never happens in the middle of a statement, and it is not a stop-the-world pause. It happens at a task boundary — a point where the runtime can call back into your program, because the walk that enumerates a block’s edges is generated code and the guest cannot re-enter itself from inside a host call holding its memory. So each chunk is atomic with respect to your program: a subgraph is scanned, every count restored, and the decision taken before your code resumes.
Two things ask for one, and neither of them is you having to remember:
- The allocator asks, and this is the one that matters in a running service. Needing a new segment means the size class has no partial segment left, which is exactly memory pressure — so the allocator sweeps what it can, requests a scan, and the request buys the next allocation rather than this one. There is no tuning constant: it scans until the candidate buffer empties or the allocation can be served. A program that has never allocated a cycle-capable block never runs the pacer at all — two counters in the allocator’s control block make that case free.
- The program asks, with
intrinsics.gc_now(). That request sets a flag and returns, for the same reason: the walk cannot run inside a host call that is holding your memory. The runtime honours it at the next boundary.
A short program that does neither simply ends with its cycle still built. That
is not a leak in any sense that costs anything: the instance’s whole arena goes
back as one allocation. The commands that check a run for leaks — atoll run,
atoll test, atoll bench, the REPL, the debuggers and a program built with
atoll build --binary — treat it that way.
When a run ends still holding blocks, each asks for one last collection and
reports only what survives it. The cycle is garbage by then and is gone. A
block something still counts — one taken with intrinsics.alloc and never
given back, say — survives the collection and is reported:
atoll run: `main` failed: `main` returned 42 and left 1 block(s)
and 0 host call(s) unreleasedThat last collection belongs to the commands, not to the runtime. A program embedded through the runtime directly ends its run with the cycle still built and its two blocks still live, because nothing asked for a scan. Add the request and the collection happens inside the run itself:
import intrinsics
struct Node {
v int
next ^Node?
}
fn main() int {
mut a: ^Node = ^Node { v: 21, next: None }
b: ^Node = ^Node { v: 21, next: Some(a) }
a.next = Some(b)
intrinsics.gc_now()
return a.v + b.v
}collected=2 is the part worth reading. The heap comes back to empty either
way — a program that never built a cycle also ends at zero — so the number of
blocks the scan gave back is the only thing that separates collected from
never allocated in the first place.
Production code should not call gc_now() in a loop. It exists for tests,
for a pathological batch that builds and abandons a large graph in one go, and
for exactly the case above: proving in a short program that a collection
happened. A service with a steady working set should let the allocator pace it.
Closures build cycles too
A closure is a code pointer plus an environment — a block holding what it captured. That environment is the one block in a program with no declared type, so the filter that decides which types can take part in a cycle cannot see it; closures get a second filter of their own, over which closure can reach which.
What matters when writing one is that a capture is an edge whether or not the
captured name was mut. A ^T is an address and the fields behind it are
mutable to anyone holding it, so the write that closes the cycle is usually not
the capturing frame’s:
import intrinsics
struct N {
v int
cb fn() -> int
}
fn install(host &mut N, seen ^N) void {
host.cb = () => seen.v
}
fn main() int {
mut n: ^N = ^N { v: 5, cb: () => 0 }
install(&mut n, n) // one node as both arguments
intrinsics.gc_now()
return n.v
}seen is not mut and nothing retargets it; host is, and the caller passes
one node as both. The node and the environment hold each other, and both are
reclaimed. Until recently the environment was never a node in the graph at all,
so a program shaped like this leaked two blocks per call and gc_now()
reclaimed nothing.
A closure stored in a struct field, inside a collection, is ordinary:
struct Rule {
accept fn(int) -> bool
}
fn at_least(threshold int) Rule {
return Rule { accept: (n int) => n >= threshold }
}
fn main() int {
mut rules: []Rule = []
rules.push(at_least(3))
rules.push(at_least(5))
mut hits: int = 0
for r in rules {
if (r.accept)(4) { hits = hits + 1 }
}
return hits
}The filter is conservative, on purpose
Which types can take part in a cycle is decided statically, over pointer
edges only — ^T, ^T?, and pointers reached through a collection’s
type arguments. Containment by value is not an edge, because every collection
mutator copies before writing to a shared buffer: to write a reference to a
block into that block you must be holding the reference, which makes the
block shared and sends the write down the copying path.
The two ways the filter can be wrong are not symmetric:
| It says | Costs |
|---|---|
| not cyclic when it is | an unbounded leak, and a Drop body that never runs |
| cyclic when it is not | a wasted slot in the collector’s buffer |
So every case it cannot decide answers yes. This is also why there is no
decorator to tell it otherwise: an unchecked promise is the one input that
could push it the other way, and that direction frees live memory. @cycle_free
was such a decorator, and writing it is now ATOLL1067.
What a cycle costs while it exists
A type that can be on a cycle is allocated from a parallel ladder of size classes, and its block carries a longer header — the candidate link, the scan link, the shadow count and the walker id, all ahead of the ordinary fields so that the reference count stays at the offset emitted code already knows. Whether a block is on that ladder is read from the pointer’s own tag, with no memory access.
Drop bodies still run when a cycle is collected, once each. What a collection
changes is when, not whether. A body may give back only what its type keeps
hidden from every walk (ATOLL1091, on Traits),
so nothing it gives back is a block the collector frees.
Read next
- Ownership — the counts, the shadow count, the candidate buffer and the sweep.
- Destruction — where a release goes when counting alone is enough, which is nearly always.
- Closures — captures, environments and escape.