Search code examples
memorylualocal

Do local variables in lua occupy memory after end of scope?


--script.lua local x = 0

--main.lua dofile("script.lua")

Now we are out of script.lua scope, so we can't directly access x variable. But it still exists and its definition occupies memory even if x == nil. How can I delete x completely? Or How can I load thousands of scripts to do some stuff and release these scripts one by one completely? I dont need their locals in my memory anymore

The only thing that worked is BAD EVIL globals. If i use global x = 0, then x = nil do what i want, but every pro lua user keep saying that globals are bad.


Solution

  • Those locals are still referenced by the global functions.

    [...] inaccessible from Lua means that neither a variable nor another live object refer to the object.

    And that reference is, if I understood correctly, a upvalue:

    When a C function is created, it is possible to associate some values with it, thus creating a C closure [...]; these values are called upvalues and are accessible to the function whenever it is called.