lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Sun, Apr 30, 2023 at 3:58 AM Heqing HUANG <hhuangaz@cse.ust.hk> wrote:
However, since x[1] is empty, should it be false and directly terminate the loop?



You never allocate anything. The garbage collector runs when you allocate objects. The contents of x[1] are never collected, and the loop continues forever.

The following program prints '47' on my system, so it took 47 iterations before the garbage collector removed x[1].

local x = setmetatable({{}}, {__mode = "v" })
local A = 0
while x[1] do
    local _ = {}
    A = A + 1
end
print(A)
 
The difference is the "local _ = {}" statement which allocates a table every iteration which also runs the GC.