lua-users home
lua-l archive

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


On 9 July 2012 22:56, John Dunn <John_Dunn@qscaudio.com> wrote:
> Our app allows users to register a Timer function which gets called periodically. I've noticed that some patterns of Lua code cause memory to grow to the point that the OS starts killing processes as if the garbage collector isn't being run. Other seemingly similar Lua code has flat memory usage.
>
> The C code looks something like this ( simplified but basically the same )

>So the only difference is that in once case the function assigned to SetHandler is contained within the >function and in the other case it isn't.
There is no "contained" function as you suggest yet there are upvalues
and globals, the main difference between between the code ( besides
explicitly calling the gc) is that code sample two and three create
new closures each time "A" is called

> This code will run fine with no memory growth
>
> function A_callback()
>   A()
> End
>
> function A()
>   Timer.SetHandler( A_callback )
> end
>
> Timer.Start( .1 )
> A()
>
That code reuses global functions

> This code has unbounded memory growth
>
> function A()
>   function A_callback()
>     A()
>   end
>   Timer.SetHandler( A_callback )
> end
>
> Timer.Start( .1 )
> A()
>
This code is creating a new global closure (A_callback) with A as the
upvalue each time it is called and it is unrefed using the C API

> This code ( basically above, but manually calling the gc ) has flat memory usage
>
> function A()
>   function A_callback()
>     A()
>   end
>   Timer.SetHandler( A_callback )
>   collectgarbage("collect")
> end
>
> Timer.Start( .1 )
> A()
>
This code creates a new global closure like the previous yet and then
collected in Lua.

> So the only difference is that in once case the function assigned to SetHandler is contained within the function and in the other case it isn't. I could buy that somehow having the function be a child would cause an unexpected reference but then I don't understand why manually calling collectgarbage fixes anything. Any ideas what might be going on?
>
>
Have you disabled the garbage collector somewhere else?
Is the static getting set to another value?
Do you know there is a value for an invalid reference (actually two)
LUA_NOREF and LUA_REFNIL?

>From the looks of it I do not see a leaking reference, if you leave
the code running does the memory decrease or only increase?

Liam