how to deal with this memory leak in lua?

linux, lua, macos

Solution

I can reproduce your results. I'm not sure what Activity Monitor really reports but Lua thinks it has released all the memory:

collectgarbage("collect")
local m0=collectgarbage("count"); print(m0)
a = {}
for i = 1, 999999 do
    a[i] = {}
end
local m1=collectgarbage("count"); print(m1)
a = nil
collectgarbage("collect")
local m2=collectgarbage("count"); print(m2)

I get this output

22.55078125
78906.55078125
22.64453125

Why Activity Monitor still reports 25Mb beats me. I think it's just because `free` returns memory back to the process, not back to the operating system.

Problem

My machine is mac mini(2011) osx10.7.4 first of all. I download the lua-5.2.2 from lua.org, unzip it, and ``` $ make macosx $ make install ``` then i run it ``` $ lua Lua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio > ``` The memory usage now is 816KB (from Activity Monitor) And then I type code below for malloc... ``` a = {} for i = 1, 999999 do a[i] = {} end ``` The memory usage growth to 79.0MB And then I type code below for dealloc... ``` a = nil collectgarbage("collect") ``` The memory usage down to 25.0MB Ok. Here is my question. Why there are 25MB left? How to release or recycle them?

Original source