Page 11 of 12

Re: Massive memory optimization: flash functions! (+SPI SSD1

PostPosted: Wed Apr 27, 2016 12:20 pm
by zivimo
Ahh OK,

I see. I wanted to test your flash functions and obviously I constructed the worst example code one could make :). OK will try to make a better example with real code instead of dummy strings.

But tell me: The functuions are stored in flash (as files) and, if I got you right, only if they get called are loaded into RAM or are they executed directly from flash (so no execution time heap usage)? If first applies then, as you said, I guess its a good idea to set the functions after call to nil again and collectgarbage()?

Thanks again

Re: Massive memory optimization: flash functions! (+SPI SSD1

PostPosted: Thu Apr 28, 2016 8:26 am
by TerryE
My FAQ linked below explains the process. Require or one of the load functions will load the code n Flash into RAM. Once dead, the GC will remove unreferenced code, but the key point here is that it must be unreferenced.

Also note that the current dev builds tailored to the modules that you use (use the cloud builder if you don't build your own firmware images) have roughly 45Kb RAM free, a bit better than the ~15Kb on the 0.96 builds.

Re: Massive memory optimization: flash functions! (+SPI SSD1

PostPosted: Thu Apr 28, 2016 12:27 pm
by dpwhittaker
Great FAQ, TerryE... I'll have to read it more completely when I get back to esp8266 development.

To answer the question, the functions are stored in flash, yes, and they are loaded into memory for execution. However, they are only returned from the __index metamethod (the method that is called whenever the key does not exist in the table), and never assigned to any variable, so they are out of scope and available for GC as soon as they finish executing. The flashMod function saves the function to disk (string.dump) and then sets the key to nil in the table, so that it will hit the __index metamethod to load the function.

Looks like NodeMCU has come a long way since I left off... I look forward to trying it out again (though with a Pi Zero as the primary node in the network and the ESP8266 as a programmable slave rather than trying to make the tiny ESP8266 the entire system).

Re: Massive memory optimization: flash functions! (+SPI SSD1

PostPosted: Thu Apr 28, 2016 6:33 pm
by TerryE
@dpwhittaker, A lot has happened in this last year. I've switched my FLOSS contributions to working as part of the NodeMCU committer team and we've made major enhancements to the codebase. Johny Mattsons' unaligned fetch exception handler allowed us to move a lot of the firmware from RAM to Flash and my PackedLineInfo patch effectively eliminated the debug overhead. We've alse fixed a lot of leak bugs and sorted out some architectural flaws relating to ISR handling. All-in-all the project is a lot healthier now and we are still sorting out issues which will really improve application stability.

BTW my FAQ is now really out of date and one of my TODOs is a complete rewrite.