TerryE wrote:The problem is your function listap. It's not a local function so the name is registered in the globals table, and therefore the memory for the code and function headers can't be garbage collected. If you have Lua installed on your PC, then you can do a luac -l of the dofile code to see what is generated.
If you want resources GCed, then declare them local.
Avoid writing stuff to flash, because it has a limited write life. If you do want to write configuration data to flash then at least check that it's changed before writing it. I know this is two pass, but your esp8266 will last a lot longer if you adopt this strategy.
Hello TerryE,
At first I thought it was an issue with it not being available for garbage collection so I tried just declaring the function and then setting it to nil and I got most of my memory back minus 272 bytes so I tried adding local to the declaration and got it all back. I then decided to test it out with wifi.sta.getap() back in CheckAP.lua...
I changed the CheckAp.lua to:
local listap= function(t)
if type(t)~="table" then
print("not table")
return
end
for key,value in pairs(t) do
tmr.wdclr()
--if string.find(key , "myssid") then
local _SSID = key
local _BSSID=""
local _RSSI=""
local _enc=""
local _chan=""
_enc, _RSSI, _BSSID, _chan = string.match(value,
"(%d),(-?%d+),(%x%x:%x%x:%x%x:%x%x:%x%x:%x%x),(%d+)")
print(_SSID..":\t\t".._BSSID..",".._RSSI.."\n")
_enc=nil _chan=nil
_SSID=nil _RSSI=nil _BSSID=nil
--end
end
end
wifi.setmode(wifi.STATION)
wifi.sta.getap(listap)
listap=nil
and the result was as follows
=node.heap()
20256
> dofile("CheckAP.lua")
>myssid: 1a:fe:34:xx:xx:xx,-1
xfinitywifi: 96:1a:ca:xx:xx:xx,-91
Linksys_dd-wrt: 00:1d:7e:xx:xx:xx,-64
=node.heap()
17952
>
I observed that the addition of local to the listap function brought the memory use from 2528 bytes down to 2272 used by the execution of CheckAP.lua, but didn't solve the memory use issue so then I thought it might be that the wifi.sta.getap() function is not releasing the memory. so i then copied the contents CheckAP.lua to a new file to test with named listap_test.lua and modified it.
listap_test.lua:
local T={}
T["myssid"]="4,-12,13:37:13:37:13:37,6"
T["MadeUpAP_1"]="4,-38,1A:3B:BE:EF:42:62,6"
T["MadeUpAP_2"]="4,-42,80:08:80:08:80:08,6"
T["MadeUpAP_3"]="4,-42,01:55:58:67:53:09,6"
local listap= function(t)
if type(t)~="table" then
print("not table")
return
end
for key,value in pairs(t) do
tmr.wdclr()
--if string.find(key , "myssid") then
local _SSID = key
local _BSSID=""
local _RSSI=""
local _enc=""
local _chan=""
_enc, _RSSI, _BSSID, _chan = string.match(value,
"(%d),(-?%d+),(%x%x:%x%x:%x%x:%x%x:%x%x:%x%x),(%d+)")
print(_SSID..":\t\t".._BSSID..",".._RSSI.."\n")
_enc=nil _chan=nil
_SSID=nil _RSSI=nil _BSSID=nil
--end
end
end
listap(T)
listap=nil
T=nil
The result of this test was:
=node.heap()
20256
> dofile("listap_test.lua")
myssid: 13:37:13:37:13:37,-12
MadeUpAP_3: 01:55:58:67:53:09,-42
MadeUpAP_1: 1A:3B:BE:EF:42:62,-38
MadeUpAP_2: 80:08:80:08:80:08,-42
> =node.heap()
20088
>
I found that my memory use had drastically reduced from 2272 to 168 bytes after running the modified version (listap_test.lua). which brings me to the conclusion that the issue must be in the wifi.sta.getap() function.
As for a workaround for this issue to get the memory back I have no idea where to start whether it be in my lua script or in the nodemcu source code.
Maybe I need to post this in the issues section of the nodemcu github...