lua-users home
lua-l archive

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


Michael Broughton wrote:
This is normal. Lua grows it memory pool as needed, it does not reduce it. also, collectgarbage("count") reports the current size of Lua's memory pool, not how much of the pool is being used.

Not... entirely... correct on either count!

t = {}
for n = 1,10000 do
    table.insert(t,n)
end
collectgarbage("collect")
print("MEM:",collectgarbage("count"))
for n = 1, #t-1 do
    table.remove(t, 1)
 end
t.foo= "bar" -- <---- LOOK!  MAGIC!
collectgarbage("collect")
print("MEM:",collectgarbage("count"))
table.foreach(t,print)

prints:
MEM:    210.0732421875
MEM:    18.1123046875
1       10000
foo     bar

The (well-discussed, but perhaps not documented, as an
implementation detail) reason is that Lua only considers
shrinking a table's allocation on a non-nil new-index
write.  (Empirically only a non-array non-nil new-index
write, at that.)

Cheers,
--Adam