lua-users home
lua-l archive

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


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?

On Thu, Jun 4, 2015 at 2:50 PM, Eike Decker <zet23t@googlemail.com> wrote:
As far as I know, tables are never reduced in size. I suspect that this is because otherwise iteration and deletion would not work as expected. A table resize causes a rehash of the content which leads to a change of order of elements which in turn would then make iteration "difficult". This would make sense in the context that iteration via next() is not stable when elements are inserted into the table (which may cause a resize). 

To release the memory of a table, you'll have to create a new one and copy the content. 

2015-06-04 23:02 GMT+02:00 Mark Hamburg <mhamburg.ml@gmail.com>:
As I understand it, erasing never causes a structure to get smaller. I forget which operations will cause a structure to compact.

Mark

> On Jun 4, 2015, at 12:56 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>
> If you create a sequence with length 2^n, erase all the elements
> except those with keys 1,2,4,8,16,...,2^n, and call collectgarbage(),
> Lua does not release the memory.
>
> 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("memory in use before 'collectgarbage()': "..(collectgarbage"count"))
> collectgarbage()
> print("memory in use after 'collectgarbage()': "..(collectgarbage"count"))
>
> Output:
>
> memory in use before 'collectgarbage()': 16409.73828125
> memory in use after 'collectgarbage()': 16406.772460938
>
> There's probably some subtlety I don't understand.
>





--
Brigham Toskin