> some MCUs have STOP mode with all clock sources disabled while all the internal RAM is kept powered
The new Raspberry Pi chips do this (RP2040 RP2350 with the RP2350 having an actual stop mode).
I'm using the esp32-s3 which has a deep sleep mode when only the
co processor is running and a light sleep mode when the main RAM
is being kept alive. The thing I wanna program is a "smartwatch" Watchy
from SQFMI which is just a small board with an E Ink display
slapped on it.
Being low power and all, it lasts a week before the battery runs
out, using the original firmware. I don't wanna sacrifice that for
programmable convenience.
These draw less than 1 microamp in this state. With the RTC doing a wake up every minute to poll sensors, battery life is in months. So lots of way to save power across the long term
--
That is quite low, the datasheet for the esp32-s3 says it draws 7 microamps in deepsleep and 240 microamps in light sleep.
~ Sewbacca
I didn't think of that, I did some more testing and wanna share what I've found:
- Running Lua without anything draws 7-8KB - Running just the base and nothing else about 9-10KB - Running a simple fibonacci program that prints the first 20 numbers 11-12KB (using only _G) - Running a simple terminal ascii clock updating constantly using a busy loop with os, string and io loaded uses just below 16KB (calling collectgarbage() every cycle)
The sleep resistant memory of the esp32-s3 has 16KB in total, so I could just barely run a simple terminal ascii clock within it. The main benefit of pausing Lua instead of restarting it every time would be that complicated programs would not have to worry about which data has to be stored and when and how it should be reconstructed.
Serializing threads could circumvent this issue by only
serializing non static data (i.e. no standard packages _G etc.) or
giving it unique ids derived of their global variable name, if
they are referenced from a local.
We can serialize almost every data type in Lua, even functions thanks to string.dump and the debug API. coroutines however seem to be the only exception, despite being transparent to the debug API.
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/55004919-cee1-4686-8dc1-17522c92a14f%40kolabnow.com.
Using Lua 5.5 should in any way bring quite a bit of RAM savings, if you know how to compile your text files to bin files externally, and then load down BIN code... ..
I reran a simple test with Lua 5.5, comparing a huge program with all packages disabled, which does nothing, but uses a whole bunch of randomly generated constant strings, using either the precompiled .luac binary or the source file. I ran the program both with 5.5 and 5.4:
Lua 5.5.0 Copyright (C) 1994-2025 Lua.org, PUC-Rio pre-compiled: max mem used 9051760 source chunk: max mem used 15796118 Lua 5.4.8 Copyright (C) 1994-2025 Lua.org, PUC-Rio pre-compiled: max mem used 7872161 source chunk: max mem used 12650342
I used lua 5.4.8 and the beta from https://lua.org/work, so not the github version. Apparently Lua 5.4 is better in saving some memory, even though Lua 5.5 should save more space if it uses lua_pushexternalstring.
I'm probably doing something wrong, because .cursize appears to be negative (on both versions) after closing the state, instead of 0 what I would've expected, so here is the relevant code snippet I used to measure used memory (by modifying lua.c).
typedef struct info {
ptrdiff_t maxsize;
ptrdiff_t cursize;
} info;
void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
info* i = ud;
i->cursize += nsize;
i->cursize -= osize;
i->maxsize = i->cursize > i->maxsize ? i->cursize : i->maxsize;
if (nsize == 0) {
free(ptr);
return NULL;
}
else
return realloc(ptr, nsize);
}
int main (int argc, char **argv) {
int status, result;
info used = { .cursize = 0, .maxsize = 0 };
lua_State *L = lua_newstate(luaL_alloc, &used, luaL_makeseed(NULL)); /* create state */
But 16kiloByte RAM is really VERY few in any case. Typically it would be better, if during sleep phases you run a special C code which is very restricted. And then during wakeup only you run Lua, and I hope during this wake up phases hopefully a bit more than 16kB RAM :).
I also tried running a small program with Lua 5.5 and got no memory benefit beyond what I've already found by not loading any unnecessary libraries, so I guess for very small applications, the new lua_pushexternalstringstring is negligible.