A development task that prints Lua heap, bitmap memory and the lowest main-stack headroom every five seconds, with the change since the previous line. Run it next to your own script in the simulator and watch the console while you use your script.
Each line looks like this (numbers illustrative; yours depend on radio and scripts):
-- Memory monitor: a background task that prints Lua heap, bitmap RAM and the-- lowest main-stack headroom seen, every few seconds, with deltas.-- Enable it under Model > Lua while developing; watch the simulator console.-- https://frskyrc.github.io/ethos-lua-doc/cookbook/memory-monitor/localINTERVAL=5-- secondslocalnextAt=0localprevLua,prevBmp,prevHeaplocalminStacklocalfunctionkb(bytes)returnstring.format("%.1fkB",(bytesor0)/1024)endlocalfunctiondelta(now,prev)ifnotprevornotnowthenreturn""endlocald=(now-prev)/1024returnstring.format(" (%+.1f)",d)endlocalfunctionwakeup()localnow=os.clock()ifnow<nextAtthenreturnendnextAt=now+INTERVALlocalmem=system.getMemoryUsageandsystem.getMemoryUsage()or{}localheap=collectgarbage("count")*1024-- live + not-yet-collected garbagelocalstack=tonumber(mem.mainStackAvailable)ifstackand(notminStackorstack<minStack)thenminStack=stackendprint(string.format("[mem] luaFree=%s%s bmpFree=%s%s heapUsed=%s%s minStack=%s",kb(mem.luaRamAvailable),delta(mem.luaRamAvailable,prevLua),kb(mem.luaBitmapsRamAvailable),delta(mem.luaBitmapsRamAvailable,prevBmp),kb(heap),delta(heap,prevHeap),minStackandkb(minStack)or"-"))prevLua,prevBmp,prevHeap=mem.luaRamAvailable,mem.luaBitmapsRamAvailable,heapendlocalfunctioninit()system.registerTask({key="memmon",name="Memory monitor",wakeup=wakeup})endreturn{init=init}