lua-users home
lua-l archive

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


2010/4/12 bravefly <bravefly@gmail.com>:
> But GC can't collect all the memory used by last lua file.

Why not? You have to figure out why the memory isn't collected.

Possibly, it's because the last file created global vars, and the gc
doesn't know that they will never be used again, so keeps them around?

If that's the case, then try useing setfenv() to create a global env
for just the last file, then discard it after running the file.

Something like:

f,emsg = loadfile(filename)
if not f then
  print(emsg)
else
  local g = setmetatable({}, {__index=_G})
  setfenv(f, g)
  local ok,emsg = xpcall(f)
  if not ok then
    print(emsg)
  end
  g = nil
  collectgarbage()
  collectgarbage()
  collectgarbage()
end

(typed from memory, YMMV)

Hao's suggestion to tune the gc to collect more often is a good one, too.

Sam