Lua Collects Blocks and Functions
I did a brief experiment today to make sure Lua garbage-collects blocks and functions. It turns out: it does. That’s good news, because it opens the door for scripts that JIT data into Lua code for the interpreter to run directly, and then throw the code blocks away.
-- let's make a TON of loaded strings...
for x=1,300000 do
local v =
load("function dummy(n) print(n+" .. x .. ") end " ..
"dummy(" .. x .. ")")
v()
end
dummy(123) -- can call the function from the program, too
io.read('l')
This script runs in constant memory, and tests three things I wanted to make sure I understood:
- Blocks are garbage-collected
- Redefinitions of the same function name make the old version collectible
- Global functions defined in loaded blocks are accessible outside the block
(as long as I don’t put a custom environment on the
loadcall)