Current Lua downloadable firmware will be posted here

User avatar
By vlast3k
#23929 I am fighting with a simple program and i've reduced it to a following method
local buf ={}

function memTest(size)
tmr.stop(3);
tmr.alarm(3, 50, 1, function()
table.insert(buf,"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890")
collectgarbage()
print(node.heap())
end)

end

which fails with "not enogh memory" while node.heap() says 13000.
I started with a more complex program, that used network and so on, but it used to restart even on statements like print("a"..20), and i decided the problem may be that the amount of free memory was ~5000 bytes.
Now with the simpler proogram if fails even sooner :(

Any idea what i may be missing?
User avatar
By TerryE
#24142 The 200 char string isn't the issue, as all strings are interned either at compile in the case of constant strings, or at runtime in the case of dynamically computed ones. What this means is that all unique strings are stored in an internal string table and Lua uses an integer key reference during execution, so referencing and comparing strings is cheap but creating them is expensive in runtime cost.

So your time alarm callback adds another entry to buf every 50 mSec or so until you run out of memory, so it is gong to fail at some point. The problem is that the memory can become very fragmented. What is probaby killing you here is the hash table that Lua uses internally to store the array entries. A simple doubling algo is used so when you get to 128 entries, the table routines allocate a new hash entry block for 256 entries, copy across the 128 existing entries and then frees the old one. Then again at 256->512 etc.

If the memory pool is fragmented, then you might well have 14K free but with no slot big enough to hold the new hash entry block.

The trick is to keep sufficient headroom to stop over fragmentation. Use a custom build to include only the hardware libraries that you need. We've also got a number of optimisations in development which will effectively free up a lot of RAM and give you 2-3x more headroom.