The Lua scripts do not have enough memory
What ArduPilot says:
- Insufficent memory loading …
- Lua: Unable to allocate a heap
- Lua: Couldn't allocate a lua state
- Required SCR_HEAP_SIZE over N
All scripts share one block of memory of SCR_HEAP_SIZE bytes (by default 43 KB, 100 KB or 200 KB, by the size of the board). “Insufficent memory loading FILE”: that script did not fit and is not running. “Unable to allocate a heap” and “Couldn't allocate a lua state”: the block itself could not be had, so no script runs. “Required SCR_HEAP_SIZE over N”: while armed, a script needed more than the block holds, and ArduPilot lent it memory from the board's free memory to keep it alive; N is the total it has reached. That warning then blocks the next arming.
What to do, most likely first
SCR_HEAP_SIZEis too small for the scripts you run.CheckSet
SCR_DEBUG_OPTSbit 1 for a while: after every run of a script a line “Lua: Time: … Mem: …” shows the memory in use.FixRaise
SCR_HEAP_SIZEwell above the N of the message (half as much again is a fair start) and reboot. Clear the debug bit afterwards.The board has less free memory than
SCR_HEAP_SIZEasks for.Check“Unable to allocate a heap” right at start-up; the ground station shows the free memory.
FixRun fewer or smaller scripts, switch off features you do not use, and set
SCR_HEAP_SIZEto what really is free.A script keeps taking memory and never gives it back.
CheckWith the debug bit set, the first Mem number grows run after run.
FixCorrect the script: tables that only grow are the usual reason.
Memory is lent only while armed, and only while at least 20 KB of the board's memory stays free. On the ground, disarmed, the script that asks for too much stops with an error instead.
Parameters to look at
| Parameter | What it is |
|---|---|
SCR_HEAP_SIZE | Scripting Heap Size |
SCR_DEBUG_OPTS | Scripting Debug Level |
SCR_ENABLE | Enable Scripting |
Where to do it:Lua Applet Catalog
The code that sends it
Insufficent memory loading …
libraries/AP_Scripting/lua_scripts.cpp lua_scripts::load_script() · Open in ArduPilot Copter-4.7.1
Lua: Unable to allocate a heap
libraries/AP_Scripting/lua_scripts.cpp lua_scripts::run() · Open in ArduPilot Copter-4.7.1
Lua: Couldn't allocate a lua state
libraries/AP_Scripting/lua_scripts.cpp lua_scripts::run() · Open in ArduPilot Copter-4.7.1
Required SCR_HEAP_SIZE over N
libraries/AP_Scripting/lua_scripts.cpp lua_scripts::run() · Open in ArduPilot Copter-4.6.3
libraries/AP_Scripting/lua_scripts.cpp lua_scripts::run_engine() · Open in ArduPilot Copter-4.7.1