Left here for archival purposes.

User avatar
By yes8s
#3587 Ok, so I've read about some issues regarding memory leakage due to the SDK and/or Lua itself. I don't quite understand what's going on so I thought I'd do a simple test to try and understand and capture what's happening:

A simple test:
- Restart ESP
- Print a string
- Check memory
- Set a s string to "test"
- Check memory
- Print s string
- Check memory
- Set the same string to nil
- Check memory
RESULT: Memory lost!

Code: Select allnode.restart()
> "ʃ
æÿ[0C]G9ÿQˆBIb   õ[11]™RClò)û
lua: cannot open init.lua
NodeMcu 0.9.2 build 20141129  powered by Lua 5.1.4
> print(node.heap())
21408
> print("test")
test
> print(node.heap())
21408
> -- No memory used
> s = "test"
> print(node.heap())
21128
> print(s)
test
> print(node.heap())
21128
> s = nil
> print(node.heap())
21224
> -- 5 minutes later
> print(node.heap())
21224
> -- Why? What happened to the (21408 - 21224 = 184 bytes)?


This effect is worse the larger the element. If I did the same thing with a largish function and then set the function and all it's variables to nil, the lost memory is even greater.

Thoughts?
User avatar
By gerardwr
#3589 Thats a simple test, but with significant proof, thanks for sharing.

Let's wait for zeroday To comment, if he can't adress this we're all doomed!
User avatar
By yes8s
#3950
Code: Select allnode.restart()
ÄžBs°F:9ý[01]Y151RúAÂNS­DRó
NodeMcu 0.9.2 build 20141204  powered by Lua 5.1.4
lua: cannot open init.lua

> print(node.heap())
21816

> s="helloworld"

> print(s)
helloworld

> print(node.heap())
21536

> s=nil

> print(node.heap())
21624


I just tried this again after there have been two updates to supposedly fix various heap issues but I still see the same problem. Not all memory is recovered.

I also tried to do the above example from within a file. Same result - memory before executing file < memory after executing file.

@Zeroday
Is this a Lua thing, or it due to this implementation or something else (SDK)?
User avatar
By yes8s
#3951
Code: Select allnode.restart()
[04]B1äþ[13]j9ýÊnÌ:#ªøóÆ[13]1f;ÿ
NodeMcu 0.9.2 build 20141204  powered by Lua 5.1.4
lua: cannot open init.lua

> print(node.heap())
21816

> s="helloworld"

> print(node.heap())
21536

> s=nil
> print(node.heap())
21624

> s="helloworld"

> print(node.heap())
21528

> s=nil

> print(node.heap())
21624

> s="helloworld"

> print(node.heap())
21528

> print(node.heap())
21528

> s=nil

> print(node.heap())
21624
 


Ok scratch that. I just tried it again but multiple times in a row. It appears as though only the first time I execute the code do we see some apparent memory leakage. After this the memory seems to be recovered correctly.

My thinking now is that after declaring the first variable, function etc. lua uses up some additional memory to create a global variable table (kind of like a file system allocation table in computers). So once this has been created once, it stays there until there is a restart.

I'd love if someone can confirm what I have said is correct (or tell me I'm completely wrong ;) (