Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to deal with this memory leak in lua?

Tags:

linux

macos

lua

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?

like image 818
e39a562r Avatar asked Oct 02 '22 05:10

e39a562r


1 Answers

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.

like image 66
lhf Avatar answered Oct 06 '22 00:10

lhf