debug.resurrectunreachable
Resurrects unreachable objects for inspection
from "debug" import resurrectunreachable
// a reference cycle: refcounting cannot free this by itself
local squad = {}
local enemy = {}
squad.rival <- enemy
enemy.rival <- squad
squad = null
enemy = null
let found = resurrectunreachable()
println("found.len() > 0 =", found.len() > 0)Output:
found.len() > 0 = trueReturn value
An array of the unreachable objects it found, or null when there are none.
Notes
This runs the same mark pass as collectgarbage, but where collectgarbage would free what it finds, this function links the unreachable objects back into the VM's live chain and returns them in the array. Holding that array keeps them alive; once nothing still references it, they are unreachable again and a later collectgarbage reclaims them as usual. Use this to inspect what would be collected, such as a leaked reference cycle, without destroying it first.
Example
from "debug" import resurrectunreachable
println("resurrectunreachable() == null =", resurrectunreachable() == null) // nothing pending yet
// a reference cycle: refcounting cannot free this by itself
local a = {}
local b = {}
a.other <- b
b.other <- a
a = null
b = null
let found = resurrectunreachable()
println("type(found) =", type(found))
println("found.len() > 0 =", found.len() > 0)Output:
resurrectunreachable() == null = true
type(found) = array
found.len() > 0 = trueSee also
collectgarbage | Runs the garbage collector and returns the number of reclaimed objects |
| debug | module index |