Memory is not released after a dofile in lua

1.6k views Asked by At

I am new to lua and was working on NodeMCU. I was trying to extract data from an xml file.

Here is my xml file:

<?xml version="1.0" encoding="UTF-8"?>
<netconfig>
<mode>0</mode>
<stamac>18-FE-34-A4-4B-05</stamac>
<staip>XXX.XXX.XXX.XXX</staip>
<stanetmask>XXX.XXX.XXX.XXX</stanetmask>
<stagateway>XXX.XXX.XXX.XXX</stagateway>
<apmac>1A-FE-34-A4-4B-05</apmac>
<apip>192.168.4.1</apip>
<apnetmask>255.255.255.0</apnetmask>
<apgateway>192.168.4.1</apgateway>
<port>80</port>
<dns>XXX.XXX.XXX.XXX</dns>
<dhcp>1</dhcp>
<stacustomconfig></stacustomconfig>
<timezone>10</timezone>
<serial>0x00000001</serial>
<connssid>ESP-10767109</connssid>
<ssid></ssid>
<passwd></passwd>
<hostname>ESP-10767109</hostname>
<reboot></reboot>
<message></message>
</netconfig>

Here is my xmlparser:

return function (xmlfile, xmlword)
    file.open(xmlfile,"r")
    local eofflag = 0
    local i, j, k, l, xmloutput
    while(eofflag < 1) do
        local m = file.readline()
        if(m == nil) then
            eofflag = eofflag + 1
        elseif (string.find(m, xmlword) ~= nil) then
            i, j = string.find(m, xmlword, 1)
            i = i - 1
            j = j + 2
            k, l = string.find(m, xmlword, j)
            k = k - 3
            l = l + 1
            xmloutput = string.sub(m, j, k)
            eoffile = 1
        end
    end
    file.close()
    return xmloutput
end

I call this file by using:

local port = dofile("xmlparser.lc")("netconfig.xml", "port")

But I always endup with memory less than I started with even though I call the garbagecollector. Moreover The heap seems to decrease further if the word I am searching for is near the end of file. I also observed that if the word I am looking for is not present, the heap has the less difference I started with.

Am I missing something? and thanks for taking a look.

2

There are 2 answers

0
Mark McGinty On BEST ANSWER

Alternatively, is the XML strictly for settings/only used by your lua code? If so I found it much easier and less memory and compute intensive to create the settings file in lua syntax, and then simply execute it. Declare a global table and exec the file:

cfg = {}  
dofile("settings.lua")  

and in the settings.lua file assign members:

cfg.port = "80"  
cfg.dhcp = "1"  
cfg.mode = "0"   

You can write the table to file easily:

local buf = ""  
for mykey,myval in pairs(cfg) do  
    buf = "cfg." .. mykey .. " = \"" .. myval .. "\""  
    file.writeline(buf)  
end  

For what it's worth...

3
Mark McGinty On

Slight changes to your module:

local module =...
return function (xmlfile, xmlword)
    file.open(xmlfile,"r")
    local eofflag = 0
    local i, j, k, l, xmloutput
    while(eofflag < 1) do
        local m = file.readline()
        if(m == nil) then
            eofflag = eofflag + 1
        elseif (string.find(m, xmlword) ~= nil) then
            i, j = string.find(m, xmlword, 1)
            i = i - 1
            j = j + 2
            k, l = string.find(m, xmlword, j)
            k = k - 3
            l = l + 1
            xmloutput = string.sub(m, j, k)
            eoffile = 1
        end
    end
    file.close()

    package.loaded[module] = nil
    module = nil

    return xmloutput
end

That makes the whole thing local and dereferences the module in the packages.loaded table, allowing it to be garbage collected.

And call it using...

xmplarser = require("xmplarser")
v = xmplarser("netconfig.xml", "port")

Hope it helps.