lua-users home
lua-l archive

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


My apologies for spreading misinformation. I think I need to go do a code review now...



Adam D. Moss wrote:
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