2.0.0 Has A Memory Leak
Posted: Sun Mar 19, 2017 12:17 am
The latest built (2.0.0) has a memory leak (or at least a poorly functioning garbage collector). I upgraded from 0.9.6 to 2.0.0 and noticed an immediate speed increase. I also noticed that my server app was crashing after a couple dozen HTML page accesses. This is the exact same code and HTML sources that I was using on 0.9.6. I printed out the heap as I accessed pages and noticed the available memory was shrinking with every access. To validate this, I wrote a bare minimum server app that continually requests a page reload and displays the current heap, iteration count and LUA version number. Here is my test code:
Run this on your ESP device and enter the IP address into your browser (it doesn't care what file you are accessing). You will see that 0.9.6 starts at around 27K and stabilizes at around 24K. In my case, 2.0.0 starts at around 37K and decreases at a rate of about 200 bytes per iteration until it crashes at around 3K. I noticed that if I increase the delay before reload to about a second, 2.0.0 will stabilize at around 4K and not crash. This implies that the garbage collection is not aggressive enough. I haven't tried this on any other versions, since I don't have a build environment. Perhaps this can be as simple as upping the garbage collection priority.
Code: Select all
wifi.setmode(wifi.STATIONAP)
srv=net.createServer(net.TCP)
x=1
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local a,b,c=node.info()
client:send("HTTP/1.1 200 OK\r\nContent-type: text/html\r\nConnection: close\r\n\r\n<!DOCTYPE html><head></head><body><p>Iterations: "..x.." Heap: "..node.heap().."</p><p>Version: "..a.."."..b.."."..c.."</p><script>setTimeout('location.reload();',300);</script></body></html>")
x=x+1
end)
conn:on("sent", function() WiFiclose() end)
function WiFiclose() conn:close() collectgarbage() print(node.heap()) end
end)
Run this on your ESP device and enter the IP address into your browser (it doesn't care what file you are accessing). You will see that 0.9.6 starts at around 27K and stabilizes at around 24K. In my case, 2.0.0 starts at around 37K and decreases at a rate of about 200 bytes per iteration until it crashes at around 3K. I noticed that if I increase the delay before reload to about a second, 2.0.0 will stabilize at around 4K and not crash. This implies that the garbage collection is not aggressive enough. I haven't tried this on any other versions, since I don't have a build environment. Perhaps this can be as simple as upping the garbage collection priority.