0
votes

How to clear the heap or all memory? I need to restore state in case script fault. On fatal errors script stopping and I want to cleanup mess before reset and script fires again. 8266 works as serial port monitor for main processor and memory needed for safe execution of restoration commands.

1
Do you want to reset all global variables to the initial state? - Egor Skriptunoff

1 Answers

0
votes

Use the command collectgarbage('collect') when you need to free used memory.
The standard trigger for collectgarbage in standalone Lua is collecting automatically at 200kb.
You can change the limit to a lower value.
Refer: https://www.lua.org/manual/5.4/manual.html#2.5
...and: https://www.lua.org/manual/5.4/manual.html#pdf-collectgarbage

Example in Lua standalone...

$ lua -i
Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
> gc_example=setmetatable({},{__gc=function(tab) print(tab,'Collected at: ',collectgarbage('count')*1024) end,__name='gc_example'})
> gc_example=empty
> collectgarbage('collect')
gc_example: 0x56626fb0  Collected at:   16918.0 -- resurrect because of __gc metamethod
0
> print(collectgarbage('collect'), collectgarbage('count')*1024) -- freeing
0   16582.0