lua-users home
lua-l archive

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


> luaC_checkGC() is called throughout Lua in places that cause
> allocations, such as OP_NEWTABLE, OP_CONCAT, etc.
> 
> While reviewing places the GC may be triggered, I noticed it is also
> checked in luaD_call(). Is there a practical or theoretical reason
> this is necessary? It seems the mechanism of a function call/return
> itself doesn't create any new collectable objects that I can see.

Very good question.

Call/return does not create collectable objects, but it can grow the
stack and therefore consume memory. (The collector shrinks stacks when
possible.)

Now, when I tried to write an example, I found a sort of bug in that
check. It only does the check in C calls, but Lua calls also grow
the stack. See the attached example. (Warning: it may trash your
machine by using too much memory!)

-- Roberto
local a = {}

local function recur (x)
  if x > 0 then return recur(x - 1) + 3
  else return 1
  end
end

local lim = tonumber(arg[1]) or 10000

for i = 1, lim do
  a[i] = coroutine.wrap(recur)
end

print(" ****  **** RUNNING")

for i = 1, lim do
  a[i](lim)
  -- io.write(collectgarbage"count" * 1024, "\n")
end