Atom

Garbage collection and native ownership

Observe a live value with a weak reference

A weak reference lets you ask for an object without keeping it alive by itself. Keep an ordinary binding while you need the object.

Atom
use runtime
item = {name = 'Ada'}
weak_item = runtime.weak(item)
print(weak_item().name)
Output
Ada

item still refers to the dictionary, so the weak reference can retrieve it. The time when an unreachable object is collected is not guaranteed.

Garbage collection and weak references

Atom collects unreachable managed objects. A weak reference lets you check whether an object is still available without keeping it alive.

Heap limits and native ownership

A host can limit managed allocations. Close external resources explicitly, and keep any host roots and native pins live for as long as C needs them.

Atom uses the conservative Boehm-Demers-Weiser collector. Heap objects include strings, collections, closures, modules, packed storage and the managed parts of handles. Cycles are collectible once unreachable. Conservative scanning can retain objects through apparent references on a native stack. A weak accessor can therefore still return its target after explicit collection. runtime.memory() reports process-wide collector statistics and runtime.collect() requests collection.

A C host roots managed values it holds in native heap memory or across calls with atom_root_new. Release each root when finished. Atom-managed packed memory needs a live value or root as long as C accesses it. Hold a pinned FFI address for a native API that stores a pointer to packed storage. Use the acquisition and release functions documented for files, sockets, processes, database connections, native allocations, callbacks and libraries. Any collector finalizer is a fallback without deterministic release timing. A foreign pointer returned by a C function is generally borrowed and has the lifetime defined by that API.

An embedding host can set a process-wide collector heap limit with atom_set_heap_limit and a separate managed allocation budget for each VM with AtomConfig.heap_limit. Zero disables the corresponding limit. Per-VM accounting includes headers and charges a shared allocation to its original VM, covering managed allocations rather than all process memory. Workers inherit the limit and are charged independently. Atom attempts collection before rejecting an allocation over the per-VM limit.

Atom
use runtime
first = {}
first.loop = first
probe = runtime.weak(first)
assert(probe() == first)
first = null
runtime.collect()
print(runtime.memory().collections)