是什么导致我的程序使用NodeMCU的内存不足? [英] What is causing my program to run out of memory using NodeMCU?

查看:679
本文介绍了是什么导致我的程序使用NodeMCU的内存不足?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么我的程序内存不足吗?我在SDK 1.5.4.1 NodeMCU上使用Lua 5.1.4.这是我的代码

Can someone please tell me why my program runs out of memory? I'm using the Lua 5.1.4 on SDK 1.5.4.1 NodeMCU. Here is my code

wifi.setmode(wifi.STATION)

wifi.sta.config("asdf","xxx")

elWiFi =(wifi.sta.getip())
if elWiFi ~= nil then
    print(wifi.sta.getip())
end

if srv~=nil then
    srv:close()
end

srv=net.createServer(net.TCP) 
if srv ~= nil then
    srv:listen(6969,function(conn) 
        if conn ~= nil then
        conn:on("receive",function(sck,numbers) 
        if numbers ~= nil then 
            collectgarbage("collect")
            p = string.find(numbers, "x=") --parses TCP string
            q = string.find(numbers, "&y")
            if p ~= nill then
                if q ~=  nil then
                    x = (string.sub(numbers,p+2, q-1))
                    print("x=" .. x )       -- prints x value
                end
            end --p ~= nill
            p = string.find(numbers, "y=")
            q = string.find(numbers, "&z")
            if p ~= nil then
                if q ~=  nil then
                    y = (string.sub(numbers,p+2, q-1))
                    print("y=" .. y)          --prints y value
                end
            end --p ~= nill
            p = string.find(numbers, "z=")
            q = string.find(numbers, " H")
            if p ~= nill then
                if q ~=  nil then
                    z = (string.sub(numbers,p+2, q-1))
                    --print("z=" .. z)
                end
            end-- p ~= nill
        end --numbers ~= nil
        conn:close()
        --conn = nil

        print(collectgarbage("count")*1024)
        collectgarbage("collect")
        --print(collectgarbage("count")*1024)

        --conn:send("test\n")

        end)
        end
    end)

end

我分别在收集垃圾之前和之后解析x和y之后打印Heap内存.随着时间的推移,它会增加,并最终崩溃.

I print the Heap memory after each it parses x and y before and after collecting garbare. It increases over time and eventually crashes.

这是输出的样子

x=-0.003997802734375
y=-0.0095672607421875
6744
6744
x=-0.0029449462890625
y=-0.0099945068359375
7133
7098
.
.
.
x=-0.003662109375
y=-0.00982666015625
35309
35275
x=-0.00311279296875
y=-0.0097503662109375
35424
35389
E:M 64
PANIC: unprotected error in call to Lua API (not enough memory)

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 25936, room 16 
tail 0
chksum 0x5b
load 0x3ffe8000, len 2268, room 8 
tail 4
chksum 0xe4
load 0x3ffe88dc, len 8, room 4 
tail 4
chksum 0xd9
csum 0xd9
„ã ì ƒNì’r‚òn|ä d dld` „ãrÛ$Œ ò „  ìdà

NodeMCU custom build by frightanic.com
 branch: master
 commit: 7b83bbb2ea134cd85ac9d63108603cc02c4e20f7
 SSL: false
 modules: adc,file,gpio,net,node,ow,pwm,spi,struct,tmr,uart,websocket,wifi
 build  built on: 2016-11-15 00:43
 powered by Lua 5.1.4 on SDK 1.5.4.1(39cb9a32)
> x=-0.003997802734375

然后重启.

非常感谢您的帮助.

推荐答案

conn:on("receive")回调中,您不必引用conn变量,而必须引用sck变量,这是为什么:

Within the conn:on("receive") callback you mustn't reference the conn variable but the sck variable and here's why:

为连接"事件注册的回调保留对conn的引用,而conn保留对回调闭包的引用.这创建了一个完美的参考周期.因为那不能被垃圾收集,所以有您的内存泄漏.

The callback registered for the 'connection' event keeps a reference to conn and conn keeps reference to the callback closure. This creates a perfect a reference cycle. Because that cannot be garbage collected there's your memory leak.

此外,您不应该在事件回调中关闭连接.

Furthermore, you shouldn't close the connection within an event callback.

有关更多讨论,请参见 https://github.com/nodemcu/nodemcu-firmware /issues/1303 https://stackoverflow.com/a/37379426/131929 .

For more discussions see https://github.com/nodemcu/nodemcu-firmware/issues/1303 and https://stackoverflow.com/a/37379426/131929.

这篇关于是什么导致我的程序使用NodeMCU的内存不足?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆