Post your best Lua script examples here

User avatar
By ThomasW
#3042 Hi

First - many thanks to the authors of NodeMcu, really promising!

Now my problem: The sample code below works fine but leaks about 100-300bytes of memory on every request - finally crashing the module. Is there a workaround or is this a bug? I know I could solve it by sending the "Content-length:" header but the 'real' server will serve dynamic content and it would be expensive to calculate the length in advance...

Thomas

Code: Select allsrv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload)
        conn:send("HTTP/1.1 200 OK\r\n")
        -- conn:send("Content-length: 25\r\n")
        conn:send("Connection: close\r\n\r\n")
        conn:send("<h1> Hello, NodeMcu.</h1>")
        print(node.heap())
        conn:close()
    end)
end)
User avatar
By zioax
#3108 Probably it's a bug. I've tried also with the Content Length option but it crash also. The only mode for fix it maybe a fw update
User avatar
By zeroday
#3118
ThomasW wrote:Hi

First - many thanks to the authors of NodeMcu, really promising!

Now my problem: The sample code below works fine but leaks about 100-300bytes of memory on every request - finally crashing the module. Is there a workaround or is this a bug? I know I could solve it by sending the "Content-length:" header but the 'real' server will serve dynamic content and it would be expensive to calculate the length in advance...

Thomas

Code: Select allsrv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload)
        conn:send("HTTP/1.1 200 OK\r\n")
        -- conn:send("Content-length: 25\r\n")
        conn:send("Connection: close\r\n\r\n")
        conn:send("<h1> Hello, NodeMcu.</h1>")
        print(node.heap())
        conn:close()
    end)
end)


This is because Lua machine takes long time to collect memory(GC).
There is a
Code: Select allcollectgarbage("collect")
to force a GC in lua, but it seems not work...
If you wait like 1 minute. the ram will come back.
I am trying to find a way to collect all unused memory back immediately in Lua machine, I think I have to dive into the GC part.
That's not easy for me, so this bug will not be fixed quickly. :oops: