lua-users home
lua-l archive

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


On Thu, Jun 4, 2015 at 10:08 PM, Brigham Toskin <brighamtoskin@gmail.com> wrote:
> I think I read somewhere recently (here? the wiki?) that it will resize the
> array section not after you nil-out trailing entries, but when you next
> assign a non-nil value to an array index; it will grow or shrink it as
> appropriate. I can't seem to reproduce/measure this behavior in Lua 5.3.
> Does anyone know about this? Did I make this up?
>

I checked this with the following code:

n=20; t={}; for k=1,2^n do t[k]=k end
for k=1,#t do if math.log(k,2)%1 ~=0 then t[k]=nil end end
print("before 'collectgarbage()': "..(collectgarbage"count"))
collectgarbage()
print("after 'collectgarbage()': "..(collectgarbage"count"))
t[3] = 3
print("after 't[3] = 3': "..(collectgarbage"count"))
collectgarbage()
print("after 'collectgarbage()': "..(collectgarbage"count"))
t[#t+1] = 42
print("after 't[#t+1] = 42': "..(collectgarbage"count"))
collectgarbage()
print("after 'collectgarbage()': "..(collectgarbage"count"))

Lua 5.1:

before 'collectgarbage()': 16413.967773438
after 'collectgarbage()': 16412.232421875
after 't[3] = 3': 16412.3359375
after 'collectgarbage()': 16412.232421875
after 't[#t+1] = 42': 16412.3359375
after 'collectgarbage()': 16412.232421875

Lua 5.2:

before 'collectgarbage()': 16409.875
after 'collectgarbage()': 16407.72265625
after 't[3] = 3': 16407.94140625
after 'collectgarbage()': 16407.72265625
after 't[#t+1] = 42': 25.31640625
after 'collectgarbage()': 25.09765625

Lua 5.3:

before 'collectgarbage()': 16411.440429688
after 'collectgarbage()': 16409.84375
after 't[3] = 3': 16409.991210938
after 'collectgarbage()': 16409.84375
after 't[#t+1] = 42': 27.1162109375
after 'collectgarbage()': 26.96875

LuaJIT:

before 'collectgarbage()': 8223.2119140625
after 'collectgarbage()': 8222.0830078125
after 't[3] = 3': 8222.1396484375
after 'collectgarbage()': 8222.0830078125
after 't[#t+1] = 42': 31.0146484375
after 'collectgarbage()': 30.9580078125

Terra:

before 'collectgarbage()': 8468.5908203125
after 'collectgarbage()': 8434.990234375
after 't[3] = 3': 8435.076171875
after 'collectgarbage()': 8434.990234375
after 't[#t+1] = 42': 243.951171875
after 'collectgarbage()': 243.865234375

Ravi:

before 'collectgarbage()': 16412.698242188
after 'collectgarbage()': 16411.078125
after 't[3] = 3': 16411.228515625
after 'collectgarbage()': 16411.078125
after 't[#t+1] = 42': 28.353515625
after 'collectgarbage()': 28.203125

Conclusions. Lua 5.1 doesn't free memory even after assigning to new
keys (same result for key 2*#t). All other Lua implementations tested
free memory after assigning to new key (#t+1), but don't free memory
after assigning to used (and nil'ed) key (3).

-- 


Best regards,
Boris Nagaev