Quirrel4.41.0

debug.collectgarbage

Defined in module "debug"
from "debug" import collectgarbage
collectgarbage(): int

Runs the garbage collector and returns the number of reclaimed objects

Return value

The number of collectable objects it found unreachable and freed.

Notes

Quirrel frees most objects by reference counting as soon as the last reference goes away; the collector only has work to do for a reference cycle, such as two tables that each hold a slot pointing at the other. Calling collectgarbage does not create cycles or force one to exist; it only sweeps whatever cycles are already garbage at the time it runs.

This function, and resurrectunreachable, exist only in a build that keeps a garbage collector. A build compiled with NO_GARBAGE_COLLECTOR does not register either one, so "collectgarbage" in require("debug") is false there instead of the call failing.

Example

examples/debug/collectgarbage.nut
from "debug" import collectgarbage

// two tables that reference each other: refcounting alone can never free this,
// since neither slot ever drops to zero references on its own
local a = {}
local b = {}
a.other <- b
b.other <- a
a = null
b = null

let reclaimed = collectgarbage()
println(reclaimed > 0)
Output:
true

See also

resurrectunreachableResurrects unreachable objects for inspection
debugmodule index