Sub topic 3
Reading the Memory Report After Compiling
Assume you can write and upload a basic Arduino sketch, understand variables and arrays, and use the Serial Monitor. This chapter focuses on the report printed by the Arduino IDE after compilation. The report helps you find memory problems before they appear while the board is running.
What the report shows
Compile a sketch for an Arduino Uno. Near the end of the build output, you may see:
Sketch uses 1234 bytes (3%) of program storage space. Maximum is 32256 bytes.
Global variables use 52 bytes (2%) of dynamic memory, leaving 1996 bytes for local variables. Maximum is 2048 bytes.The first line describes flash memory. Flash stores the compiled program and constant data. The Uno has 32,256 usable bytes for a normal sketch, although the physical chip contains 32 KB. The bootloader occupies part of that space.
The second line describes SRAM, which holds data while the program runs. The report counts global and static variables, then shows the SRAM left for local variables, the heap, and the stack.
| Report text | Memory area | What it includes |
|---|---|---|
| Program storage space | Flash | Program instructions and some constant data |
| Global variables | SRAM | Global, static, initialized, and zero-filled data |
| Local variables | Remaining SRAM | Stack, heap, and local data used while running |
A high flash percentage does not directly mean that the board will run out of SRAM. These are separate limits. A sketch can use little flash but nearly all available SRAM.
Reading the numbers
The percentage is calculated against the selected board's available memory:
used / maximum × 100For the example, the SRAM percentage is:
52 / 2048 × 100 = 2.5%The IDE rounds the displayed value, so it may show 2%.
The phrase “leaving 1996 bytes for local variables” can be misleading. It does not promise that 1996 bytes are safely available for one local array. That space is shared by several runtime uses:
- Local variables and function call data use the stack.
- Dynamic objects use the heap.
- Interrupt handling also needs stack space.
- The Arduino core and libraries may use SRAM internally.
The report measures memory known at compile time. It cannot predict every allocation made by String, malloc(), or a library while the sketch runs.
A practical compile-and-check workflow
Use this process whenever you select a new library or add a large data structure:
- Select the correct board under the Arduino IDE's board menu.
- Compile the sketch without uploading it.
- Record both memory lines from the output.
- Check the maximum values. They depend on the selected board.
- If SRAM usage is high, inspect global arrays, strings, and library objects first.
- Upload and test the sketch under its normal workload.
A different board selection can produce a different report even when the source code is unchanged. Always check that the selected board matches the hardware on your desk.
Following the report to the source
If flash usage is high, look for large constant tables, long functions, and libraries that are no longer needed. If global SRAM usage is high, look for large arrays, buffers, and ordinary string literals.
For example, this global array consumes SRAM:
char statusMessage[] = "Temperature sensor is ready";The report includes its storage because the array can be changed while the program runs. Later, you can move data that never changes into flash. The next chapter examines how SRAM is divided at runtime and how the reported total fits into that layout.
Exercise
A report says “Global variables use 1500 bytes, leaving 548 bytes for local variables” on an Uno. What does the 548-byte value represent, and why is it not a guaranteed safe buffer size?
Show answer
It is the remaining SRAM shared by the heap, stack, local variables, and runtime library data. It is not guaranteed safe because those parts can grow while the sketch runs.