Sub topic 13
Recognizing Out-of-Memory Symptoms
This chapter assumes that you can write and upload Arduino sketches, use variables and arrays, and read messages in the Serial Monitor. The examples target an AVR board such as the Arduino Uno, which has only 2 KB of SRAM.
A sketch can compile successfully and still fail while it runs. The compiler checks program structure and flash usage, but it cannot predict every runtime use of the heap and stack. When SRAM becomes crowded, the symptoms may look unrelated to memory.
What a heap and stack collision looks like
The SRAM used by a typical AVR sketch has three moving parts:
- Static data contains global variables and static arrays.
- The heap grows when code uses dynamic allocation, such as
Stringormalloc(). - The stack grows when functions run. It holds local variables, function arguments, and return information.
The heap usually grows upward. The stack usually grows downward. If they meet, one can overwrite the other.
Common symptoms include:
| Symptom | Likely cause |
|---|---|
| Serial output becomes corrupted | A buffer, heap object, or stack frame was overwritten |
| The board resets unexpectedly | Stack or heap data damaged control information |
| A sensor value suddenly becomes impossible | A variable was overwritten |
| A loop works briefly, then fails | Repeated allocation or increasing stack use |
| Failure occurs only with long messages | A buffer or `String` needs more SRAM |
| Adding a debug print changes the failure | The extra call changes memory layout or stack use |
These symptoms are clues, not proof. Electrical noise, bad wiring, and timing errors can produce similar behavior.
Measure free SRAM during execution
On AVR boards, a helper can estimate the gap between the heap and the current stack position.
extern int __heap_start;
extern int* __brkval;
int freeRam() {
int localVariable;
return reinterpret_cast<char*>(&localVariable) -
reinterpret_cast<char*>(__brkval == nullptr
? &__heap_start
: __brkval);
}
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(F("Free RAM: "));
Serial.println(freeRam());
delay(1000);
}A falling value after each loop suggests that the sketch keeps allocating memory. A very small value means that even a modest function call or local array could cause trouble. The exact safe margin depends on the sketch, because interrupts and nested calls also use the stack.
Reduce the risk
Start with the largest sources of memory pressure:
- Move constant text to flash with
F("text"). - Store constant lookup tables with
PROGMEM. - Replace large local arrays with smaller or global fixed-size buffers when appropriate.
- Use the smallest data type that fits the value, such as
uint8_tfor values from 0 through 255. - Avoid repeated
Stringconcatenation on small AVR boards. Prefer fixed-size character arrays and bounded functions such assnprintf(). - Avoid recursion unless you can prove that its maximum depth is small.
- Check array indexes and string lengths. A single out-of-bounds write can imitate an out-of-memory failure.
A practical diagnosis
- Record free SRAM at startup and during the operation that fails.
- Temporarily remove large buffers, long messages, and dynamic allocation.
- Add the pieces back one at a time.
- Check whether the failure follows the total memory use or a specific array access.
- Inspect every index and buffer length before assuming that the heap is the cause.
Exercise
A sketch reports 500 bytes of free SRAM at startup, but the value falls by 20 bytes on every loop until the board resets. What should you investigate first?
Show answer
Investigate repeated dynamic allocation, such as growing String objects or malloc() calls. Find the allocation that is not released or that fragments the heap, then replace it with fixed-size storage where possible.