Is IDE memory usage reliable?

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.

Are those variables actually used in the code? If not the compiler may be optimizing them away.

1 Like

Thanks, that's it. After adding this to setup() and uploading

Serial.print("xyz =  ");
Serial.println(xyz);
Serial.print("abc =  ");
Serial.println(abc);

1211 went up to 1227 at the next compile.

I hadn't realised that was a condition.

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"?

1 Like

Stack, heap,…mainly over my head.

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?

No. The percentage report simply tells you what it knows at compile-time. For memory, that's Global and static variables only.

'String' class objects use memory space that's allocated at run-time. The compiler has no way of knowing how much that is.

The compiler also has no idea how much memory will be used by local variables inside functions.

Also, the compiler has no idea how much memory will be used by local variables inside functions of the libraries that you use.

Also, the compiler has no idea how much memory is allocated at run-time by any libraries that you use.

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?

Yes it is.

You don't.

Such is embedded processing programing.
You can put print statements giving the current amount of free memory in your code.

This is a good overview is what is happening.

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.

Thanks, will study that this evening. But meanwhile it looks increasingly likely that this memory issue is the cause of my crash. Additional evidence: I'm using most of the String functions from the RTClib library:
https://github.com/adafruit/RTClib/blob/master/examples/toString/toString.ino

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.
:unamused:

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.

1 Like

Looks good, studying now, thanks.

Be aware that code in the link that is posted might contain a bug; see Classic Nano has too much RAM

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.

OK, understood.

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).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.