lua-users home
lua-l archive

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


On 28 May 2016 at 19:24, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Rodrigo Azevedo once stated:
>> The code
>>
>> N = 1.0e7
>> C = {}
>> for i=1,N do C[i] = i end
>> print(collectgarbage("count"))
>> for i=1,100*N do
>>         local a = {1,2,3,4}
>> end
>> print(collectgarbage("count"))
>>
>> prints
>>
>> 262166.11035156
>> 445760.78710938
>>
>> problem: when Lua uses lot of memory, the creation of temporary objects can
>> consume all your memory. I think CG should collect the objects fast enough,
>> but it does not. A perfectly OK program can't run because of the amount of
>> garbage to be collected consumes all memory.
>
>   The code presented does not generate any garbage so there's not much to
> collect.  To do some tests on my system, I had to change the code a bit:
>
> N = 25000
> C = {}
> for i=1,N do C[i] = i end
> print(collectgarbage("count"))
> for i=1,100*N do
>         local a = {1,2,3,4}
>         a = nil
>         if i % 1000 == 0 then
>            collectgarbage('count')
>           --collectgarbage('step')
>         end
> end
> print(collectgarbage("count"))

This got me intrigued: why is the `a = nil` necessary to make it
"generate garbage"? I thought the `{1,2,3,4}` table would go out of
scope even without it. Apart from the `if % 1000` block, shouldn't the
two codes be equivalent?

-- Hisham