logo elektroda
logo elektroda
X
logo elektroda

ESP32 NodeMCU: what value does node.heap() return when started?

TvWidget 1212 24
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18896249
    TvWidget
    Level 38  
    How much free RAM should there be after running the NodeMCU on the ESP32 ?
    That is, roughly what value should the node.heap() function return ?
  • ADVERTISEMENT
  • #2 18896278
    khoam
    Level 42  
    Quote:
    Returns the current available heap size in bytes. Note that due to fragmentation, actual allocations of this size may not be possible .
    .
    So really, I wouldn't suggest the result returned by this function.
    It's probably better to use node.egc.meminfo() .
  • #3 18896784
    TvWidget
    Level 38  
    I have a board with an Ethernet interface. If I don't start it then, as expected, I can create a string of up to 4kB in LUA code and perform various operations on it.
    When the LAN starts up, trying to create a string over 2kB ends in a no memory error and a reset. If I perform some meaningless operation requiring memory allocation before starting the Ethernet, it manages to create longer strings later.
    I am wondering where to look for the cause of the problem.
  • ADVERTISEMENT
  • #5 18897146
    TvWidget
    Level 38  
    I have already experimented with this. I have not noticed any change.
    The problem is not with a complex programme. I only have a few test instructions. The only thing that uses memory is Ethernet.
    From the code in LUA, however, there is no control over this.
  • #6 18897359
    khoam
    Level 42  
    Which Ethernet module is it specifically?

    TvWidget wrote:
    From the code in LUA, however, there is no control over this.
    .
    As you have the lua interpreter loaded, the garbage collector is also automatically started.
  • #7 18897522
    TvWidget
    Level 38  
    khoam wrote:
    What Ethernet module specifically is this?
    .
    I don't really know what the question is specifically about. The PHY is on an IP101 chip.
  • #8 18897656
    khoam
    Level 42  
    Try calling the collectgarbage() function before running Ethernet.
  • #9 18897727
    TvWidget
    Level 38  
    I've done this before too.
    At the start, the NodeMCU reports 85kB of free memory. When the Ethernet starts up, 45kB is left. I wonder if this starting amount is not too low. Perhaps the problem is at the compilation stage of the NodeMCU.
  • #10 18897746
    khoam
    Level 42  
    What values are returned by collectgarbage('count') before and after running the LAN?
  • ADVERTISEMENT
  • #11 18897801
    TvWidget
    Level 38  
    Before launch 61.962890625
    After launch 62.3232421875

    Now I test run two instructions at the beginning:
    eth.on(...
    eth.ini(...
    Then I try to create a long string and perform some operation on it. This ends with the message "not enough memory". However, the Esp32 does not perform a reset.
    After deleting the first instruction, the error stops. Multiple long strings can be created. Perhaps this is a coincidence.
  • #12 18898475
    TvWidget
    Level 38  
    I don't think I quite understand how LUA works. I already know the reason for the low memory. I don't know if this is exactly the cause of the problem. However, after increasing it, the problem disappears.
    I have the bootloader code added to the iNode MCU. It contains a short code in C, which always executes, and a relatively long code in LUA. The part in LUA is just a set of functions. They are not called during normal start-up. They do, however, occupy RAM. If I convert these functions into an object, will I save memory ?
  • #13 18898489
    khoam
    Level 42  
    TvWidget wrote:
    They do, however, take up RAM. If I convert these functions into an object, do I save memory ?
    .
    If the initialisation of the object of the class also happens later, that is allocating memory for that object. I don't know if you will "save" but you will "move" in time.
  • #14 18898576
    TvWidget
    Level 38  
    As I wrote earlier, I don't know how exactly the LUA interpreter works. Logically, all source code must first be rewritten into RAM. If this code is long it will take up more memory. Let's assume that an object is defined in this code. Later, during the execution phase of the instruction, if I substitute the value nil for this object, will the RAM allocated to the code be released ?
  • #15 18899425
    khoam
    Level 42  
    The Lua script is loaded into RAM from flash before execution. It takes up more RAM if the Lua source is plain text, as the Lua VM will have to compile it to bytecode. You can pre-compile Lua sources (already in ESP itself) using the command node.compile (). Additional savings in RAM usage can be achieved using node.stripdebug ().
  • #16 18899526
    TvWidget
    Level 38  
    The way I have mentioned now seems obvious to me. This is for me a very big advantage of the LUA language. As you wrote the script is loaded into RAM before execution. However, you can later remove any part of it from memory and reclaim the space. In other words, the program can dynamically load the necessary pieces of code and remove the unnecessary ones. E.g. after exiting the bootloder, I remove everything that was or could have been used in it.
  • #17 18901370
    TvWidget
    Level 38  
    khoam wrote:
    You can precompile Lua sources (already in ESP itself) using the command node.compile ().
    .
    What does compiling the code do in practice ?
  • #18 18901424
    khoam
    Level 42  
    It is smaller in size, so it will also take up less RAM space.
  • #19 18901499
    TvWidget
    Level 38  
    The lc file is usually a bit longer than the lua file regardless of what I choose via node.stripdebug(). ESP seems to load this lc file entirely into RAM. So what is the mechanism for this memory saving ?

    By the way, I have a question about the simultaneous use of two functionalities. It is about writing a file to flash memory and BT scanning. With me, sooner or later this triggers a reset without any error message. Should this be accepted ?
  • #20 18901549
    khoam
    Level 42  
    TvWidget wrote:
    The lc file is usually a bit longer than the lua file regardless of what I choose via node.stripdebug(). ESP seems to load this lc file entirely into RAM. So what is the mechanism behind this memory saving ?
    .
    It uses less memory on the heap. On the other hand, it's best to compile lua code for yourself on a PC and then load it into the ESP.
    https://nodemcu.readthedocs.io/en/master/compiling/
  • ADVERTISEMENT
  • #21 18903525
    TvWidget
    Level 38  
    Suppose I put nil to an object containing a lot of lua code. If I display the result of the node.heap() function after some time, it is clear that the memory has been recovered. However, this does not happen immediately. So I tried executing collectgarbage(). However, this did not change anything. Is it blocking ? I.e. will the next lua instruction only execute once the garbage collection has been carried out ?
  • #22 18903537
    khoam
    Level 42  
    TvWidget wrote:
    So I tried to execute collectgarbage().
    .
    Is it immediately after assigning nil to the object?
  • #23 18903569
    TvWidget
    Level 38  
    khoam wrote:
    TvWidget wrote:
    So I tried to execute collectgarbage().
    .
    Did it immediately after assigning nil to the object?

    Yes
  • #24 18903644
    khoam
    Level 42  
    You could try setting a more "aggressive" egc behaviour: collectgarbage("setpause", 50). Set this at the beginning of the program, before the collectgarbage() calls.
  • #25 19271316
    kogiel
    Level 17  
    An old topic, but maybe someone will find it useful.
    collectgarbage() on ESP does not work, whereas you can set the limit this way:
    node.egc.setmode(node.egc.ON_MEM_LIMIT, -6144) -- Try to keep at least 6k heap available for non-Lua use

Topic summary

The discussion revolves around the memory management of the ESP32 NodeMCU, specifically the value returned by the node.heap() function after starting the NodeMCU. Users report varying amounts of free RAM, with one noting 85kB available initially, which drops to 45kB after starting the Ethernet interface. Suggestions include using node.egc.meminfo() for better memory insights, experimenting with garbage collector settings, and pre-compiling Lua scripts to save RAM. The impact of the Ethernet module on memory allocation is highlighted, with users exploring the relationship between Lua code execution and memory usage. The conversation also touches on the behavior of garbage collection and the potential for memory recovery after setting variables to nil.
Summary generated by the language model.
ADVERTISEMENT