Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Memory 是什么导致我的程序使用NodeMCU时内存不足?_Memory_Lua_Nodemcu - Fatal编程技术网

Memory 是什么导致我的程序使用NodeMCU时内存不足?

Memory 是什么导致我的程序使用NodeMCU时内存不足?,memory,lua,nodemcu,Memory,Lua,Nodemcu,有人能告诉我为什么我的程序内存不足吗?我正在SDK 1.5.4.1节点上使用Lua5.1.4。这是我的密码 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.createSer

有人能告诉我为什么我的程序内存不足吗?我正在SDK 1.5.4.1节点上使用Lua5.1.4。这是我的密码

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
在收集garbare之前和之后,每次解析x和y之后,我都会打印堆内存。它会随着时间的推移而增加,最终崩溃

下面是输出的样子

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
变量,原因如下:

为“连接”事件注册的回调保留对
conn
的引用,
conn
保留对回调关闭的引用。这将创建一个完美的参考循环。因为它不能被垃圾收集,所以内存泄漏

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


有关更多讨论,请参阅和。

网络中的upval泄漏是一个问题。避免意外upval绑定的最简单方法是提升接收方例程的声明。类似下面的内容。我还重新编码了解析器,尽管在更经济的Lua中使用了相同的语法假设。(未调试,因此可能会出现打字错误等)


听起来很奇怪。引用周期不会阻止Lua收集垃圾。@EgorSkriptunoff,否,但on函数在Lua注册表中注册CB,当前网络代码不会清除这些垃圾,因为object\uu GC函数在open upval中持续存在,因此标记和扫描不会清除它们。没有典型的Lua行为,但事实就是如此。@Marcel我不确定我是否遵守了。我没有在
conn:on(“receive”)
中引用
conn
,我甚至没有使用
sck
。除了
conn:close()
之外,即使在我删除之后,我的程序也只在3次输入后挂起。是的,您正在执行
conn:close()
。这应该是
sck:close()
@user3249979您不应该关闭连接。关闭插座是可以的,建议这样做。有用的答案,以便堆栈溢出可以将此标记为已解决。
local srv=net.createServer(net.TCP)
local function receiver(sck,numbers)
  local p={}
  for v,n in (numbers or ''):gmatch("([x-z])=(%d+)") do
    print (("%s=%s"):format(v,n))
    p[v]=n+0
  end
  -- processReq(p.x, p.y, p.z)
  print(collectgarbage("count")*1024)
  collectgarbage("collect")
  sck:close()
end

if svr then
  srv:listen(6969,function(conn) conn:on("receive",receiver) end)
end