Garbage Collection
Pyr uses a mark-sweep garbage collector for heap-allocated objects. The std/gc module gives you control over collection timing and visibility into memory usage.
Pyr's GC runs automatically at loop back-edges when memory usage crosses a threshold. For most code, you never think about it. The |
|
|
|
Allocate a bunch of strings in a loop. The GC collects unreachable objects automatically. |
|
|
|
|
|
For server workloads, combine arenas for per-request memory with GC for long-lived state. Arenas handle the fast path (bulk free on request end), GC handles everything else. |
|
$ pyr run gc.pyr start: 0 bytes, 0 collections, 0 objects after loop: 43158 bytes, 0 collections, 2011 objects after collect: 0 bytes, 1 collections, 0 objects paused: no collections during sensitive work final: 45342 bytes, 1 collections, 2013 objects |