Sub topic 3
Reading the memory report after compiling
This chapter assumes you can write and upload a sketch, know basic C++ (variables, arrays, pointers), and can use the Serial Monitor. It also assumes you know that an Uno-class board has flash, SRAM and EEPROM. Here you will learn to read the two lines the Arduino toolchain prints after every successful build, and to judge what the numbers mean for your sketch.
Where the report comes from
After the compiler and linker finish, the Arduino IDE and arduino-cli run a size tool on the resulting program. The tool reads the section sizes from the linked file and compares them with the limits of the selected board. On an Arduino Uno (ATmega328P) the output looks like this:
Sketch uses 3472 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 278 bytes (13%) of dynamic memory, leaving 1770 bytes for local variables. Maximum is 2048 bytes.The report is a compile-time summary. It describes what the linker placed in the program image. It says nothing about what happens while the sketch runs. Keep that distinction in mind throughout the chapter.
Line one: program storage space
"Program storage space" is flash. The number covers:
- Your compiled code and the code of every library function you call.
- The Arduino core (
Serial,digitalWrite(), timers and so on). - The bootloader's reserved area is subtracted from the maximum, which is why the Uno shows 32256 and not 32768. The bootloader takes 512 bytes.
- Initial values for global variables. A global such as
int counter = 5;needs a copy of the value5in flash, which is copied into SRAM at start-up. - Anything you explicitly placed in flash with
F()orPROGMEM.
If flash overflows, the build fails with a "Sketch too big" error. That is the friendly case: you find out before uploading.
Line two: global variables and dynamic memory
"Global variables" is SRAM used by static data. This is the part of SRAM that is fixed for the whole run of the program. It contains:
- Global and
staticvariables that have an initial value (the.datasection). - Global and
staticvariables without one, which start at zero (the.bsssection). - Plain string literals, such as the text in
Serial.println("Sensor ready"), because on AVR they are copied into SRAM at start-up. - Buffers that libraries declare statically.
Serialalone reserves two 64-byte ring buffers, which is 128 bytes before your code adds anything.
The report then subtracts this from the total to show "leaving N bytes for local variables". That remainder must hold both the heap and the stack for the whole run.
What the report cannot see
The memory report is blind to everything that happens at run time. The table lists what is and is not included.
| Memory use | Counted in the report? |
|---|---|
| Compiled code and library code | Yes (flash) |
| Global and static variables | Yes (SRAM) |
| Plain string literals | Yes (SRAM) |
| Local variables and function arguments | No (stack) |
| Return addresses of function calls | No (stack) |
| `malloc()`, `new`, and `String` storage | No (heap) |
| EEPROM contents | No |
So the report is a floor on SRAM use. Real peak use is the static figure plus the deepest stack plus the largest heap you ever hold at one time.
A rule of thumb for the percentage
The percentage next to "dynamic memory" is easy to misread. A value like 13% looks safe, but the stack and heap still need room. As a working guideline on a 2 KB board:
- Under about 50% static use is usually comfortable for simple sketches.
- Between 50% and 75% you should check free RAM at run time, which a later chapter covers.
- Above 75% expect trouble as soon as you add a
String, a deeper call chain or a bigger local array.
These thresholds are heuristics, not limits from the toolchain. A sketch that uses no heap and shallow calls can run safely at 90%. A sketch that builds strings dynamically can fail at 40%.
Worked example: seeing the effect of a change
Compile the following sketch and note the two lines of output.
void setup() {
Serial.begin(9600);
Serial.println("Sensor ready: calibration table loaded and verified");
}
void loop() {
}Now change one line to wrap the literal in F():
void setup() {
Serial.begin(9600);
Serial.println(F("Sensor ready: calibration table loaded and verified"));
}
void loop() {
}Compare the reports. The "Global variables" figure drops by the length of the string plus one byte for the terminating null character. For this text that is 52 bytes. The flash figure changes by a small amount, because the text is still stored in flash and the F() version uses slightly different printing code. This before-and-after comparison is the most reliable way to check whether an optimization helped. Build, record both lines, change one thing, build again.
Getting more detail than the summary
The two summary lines hide which variable or function is responsible. The AVR toolchain includes avr-size and avr-nm, which list section sizes and individual symbols. With arduino-cli, you can ask for a build directory and then inspect the linked file:
arduino-cli compile --fqbn arduino:avr:uno --build-path ./build MySketch
avr-size -C --mcu=atmega328p ./build/MySketch.ino.elf
avr-nm --size-sort -S ./build/MySketch.ino.elfThe first command compiles and keeps the build files in ./build. The second prints flash, SRAM and EEPROM totals. The third lists symbols sorted by size, so the largest globals and functions appear at the bottom. If a library you did not expect shows up near the bottom, you have found a candidate for removal or replacement.
Common errors when reading the report
- Treating "leaving N bytes" as free RAM at run time. It is the room left for heap and stack together, before either is used.
- Ignoring a low-memory warning. The toolchain prints "Low memory available, stability problems may occur" when static use is very high, near 75% in the IDE's default setting. Treat it as a real warning.
- Expecting local variables to appear. A local
char buffer[200]inside a function adds nothing to the report, but uses 200 bytes of stack when the function runs. - Comparing reports across boards or cores. Another board has different totals, a different bootloader size and sometimes a different core, so the numbers are not comparable.
- Comparing builds with different options. Changing the board, compiler flags or library versions changes the output. Compare like with like.
- Assuming a passing build means a working sketch. The report cannot detect a collision between heap and stack. It only proves that the static part fits.
Edge cases
- Dead code removal. The linker drops unused functions, so adding a library
#includewithout calling anything often changes nothing. Calling a single function can pull in a lot of code, for example floating-point printing. - Unused globals. A global that is never read may be removed by the optimizer, so adding a "test" array to see its effect may show no change. Make the program use it, for example by printing one element.
- Compiler version differences. Different AVR core versions produce slightly different totals for the same sketch. Small differences of tens of bytes are normal.
- Other boards. On boards with a different architecture, such as ARM-based ones, the wording and figures in the report differ, and the SRAM split between static data, heap and stack works differently. The reading approach is the same, but check the board's documentation.
Summary
The report gives you two reliable facts: how full flash is, and how much SRAM static data uses. It gives you no fact about the heap or stack. Use it to catch flash overflows early, to measure the effect of each change, and to decide when a run-time check is needed.
Exercise
A sketch compiles with the message "Global variables use 1650 bytes (80%) of dynamic memory, leaving 398 bytes for local variables. Maximum is 2048 bytes." The sketch then resets randomly after it starts building String objects. Explain why this can happen even though 398 bytes are reported as available, and name two changes that would give more room.
Show answer
The 398 bytes must hold both the heap and the stack, and the report counts neither. String objects allocate on the heap, which grows upward, while function calls grow the stack downward. With so little room, the two can meet and overwrite each other, which causes crashes and resets. Two changes: wrap constant text in F() or move tables to PROGMEM so static data shrinks, and replace String with fixed-size char buffers so heap use disappears. Using smaller data types for global arrays also helps.
Exercise
You add char log[300]; as a local variable inside a function, rebuild, and the "Global variables" figure does not change. Does the array use SRAM? When?
Show answer
Yes. A local array lives on the stack, so it uses 300 bytes of SRAM, but only while the function is running. The build report counts static data only, so it does not show stack use. Moving the array to a global would make it appear in the report and use SRAM permanently.