I've been assuming that one candidate I do not yet need to consider when trouble-shooting my current project is memory usage, of either type. Current status is:
Sketch uses 22106 bytes (71%) of program storage space. Maximum is 30720 bytes. Global variables use 1211 bytes (59%) of dynamic memory, leaving 837 bytes for local variables. Maximum is 2048 bytes.
(Checking, 837 does equal 41%.)
But are these IDE 1.8.19 numbers reliable? I've been surprised that recent deletions (apart from comments) didn't show a reduction. So I investigated today by simply adding a couple of global variables:
byte xyz = 123;
int abc = 256;
I'd expected to see that 1211 increase a bit, but it doesn't, so I'm clearly missing something.
Well what is not being used is not compiled, but also keep in mind that whatever is place on the heap like 'String' objects, are also not counted, but are used. The count only counts what is placed on the stack.
Nope. Space on the stack, like the heap, is used and released dynamically at run-time. Perhaps you meant: "The count only counts what is placed in the Global and Static areas"?
All I really want to know is: can I rely on those two percentages to tell me if I am getting close to their respective limits, including all aspects such as the few Strings I have deployed?
Thanks, that’s bad news! So my predictable next question is, how do I know when I’ve exceeded the capabilities of my device? And if the answer is: when a sketch crashes, such as before finishing setup(), how do I even know which of the elements you’ve listed is the cause, and proceed to fix it?
Depending on the processor, there are typically functions you can call that will return pointers to the top of the heap and bottom of the stack. Remembering that the heap grows up and the stack grows down, subtracting these two pointers will give you a reasonable estimate of the unallocated data memory … at that time.
Note: this may not work for ESP32 as that processor’s memory structure is more complex. It has multiple, non-contiguous memory segments spread across its memory map.
Commented out one of my several functions derived from those: no crash.
If I'm right, then it only became apparent as I added more and more features.
So I would have to bite the bullet and attempt converting that (very convenient and understandable code) to the classic but hard C equivalents or whatever they are called.
The page Evils of Arduino Strings tells why the String class is problematic, but also has some information on how to replace Strings with strings. Maybe helpful.
For starters, I would display the results of the freeRam() code at the end of setup; that would give a better indication than the memory usage that the IDE reports.
I’ve shown the freememory() function - freeRAM( ) ? - before the section that is presumably causing it to crash before reaching the end of setup(). Last night i was showing results around 800 bytes (cf 870 from IDE).