lua-users home
lua-l archive

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


Hi!

I am running Lua 5.2.2 in microcontroller environment, using an RTOS. The available resources are very limited (only a few kb of RAM), and I am trying to make sure that Lua will work correctly in this environment.

To stress test the whole firmware, and ensure that memory is handled correctly, I loaded a script that essentially does the following:

    index = 1
    data = "">
    -- This function is called repeatedly, till memory exhaustion.
    function test()
        data[index] = 5
        data[index + 1] = 6
        data[index + 2] = 7
        index = index + 3
    end

What I expect from this function is to make Lua run out of memory (heap memory, due to the continuous allocations).

To my surprise the program failed in another way. As the memory usage is increased, Lua calls the garbage collector to free up some memory. Each time the garbage collector is running, it is using more and more stack, leading to stack overflow long before the heap is exhausted. It seems that no matter how much I increase the stack size, the garbage collector will eat it all very soon.

Note that I am talking here about the C runtime stack, the actual memory that the CPU accesses on the hardware, not the Lua API stack.

Is this the expected behavior? I would expect that as the garbage collector has specific work to do, it should consume the same memory for every execution.

Is there any way to handle garbage collections to improve this situation?