5

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?

e39a562r
  • 75
  • 7
  • Lua will consume some memory itself. – Raptor Dec 06 '13 at 07:31
  • 1
    @ShivanRaptor But the originally reported usage was 816KB, while the end reported usage after the GC is 25MB (over 30x more!) - the devil (and more interesting bits) are in the details. – user2864740 Dec 06 '13 at 07:32
  • 6
    Nit: it's not a memory leak per se. If *repeatedly* running the code led to an escalating memory footprint, that'd be another story. – user2864740 Dec 06 '13 at 07:34

1 Answers1

4

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.

lhf
  • 70,581
  • 9
  • 108
  • 149