lua-users home
lua-l archive

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


Shmuel Zeigerman wrote:
> This is how my application was doing originally. But unfortunately, Lua 
> tables used as dictionaries won't always reclaim memory when their entries 
> are deleted. After several millions creations and deletions of userdata, 
> this (empty!) table was holding > 100 MBytes of RAM that could not be 
> reclaimed with garbage collector.

This has been discussed quite often in the past. Lua tables only
shrink when they need to expand. Sounds strange, but this is the
only way to allow deletions during traversals.

If you're storing all your userdatas in the array part, a simple
't.foo = nil' triggers the shrinking. If you'r storing them in
the hash part, a 't[1] = nil' will do.

Example:
  collectgarbage()
  local t={}
  print(gcinfo())
  for i=1,1e6 do t[i]=true end
  for i=1,1e6 do t[i]=nil end
  collectgarbage()
  print(gcinfo())
  t.foo=nil
  print(gcinfo())

Output:
17
12305
17

--Mike