Sub topic 7
Measuring Free RAM While the Sketch Runs
This chapter assumes you can write and upload Arduino sketches, use the Serial Monitor, and understand variables, arrays, functions, and pointers. The examples target an AVR-based board such as the Arduino Uno.
The compiler reports how much SRAM global data uses before the sketch starts. That number does not show how much SRAM remains during execution. Local variables, function calls, and dynamic allocations use SRAM while the program runs. A run-time measurement helps reveal whether these uses are approaching each other.
What the measurement means
On a typical AVR Arduino, SRAM is used in three important regions:
- Static data contains global and
staticvariables. - The heap grows upward when code uses dynamic allocation, including some
Stringoperations. - The stack grows downward for local variables, function arguments, and return information.
Free RAM is the gap between the top of the heap and the current stack position. The following helper estimates that gap:
This helper is specific to the AVR memory layout:
extern int __heap_start;
extern int *__brkval;
int freeRam() {
int v;
return (int)&v -
(__brkval == 0 ? (int)&__heap_start : (int)__brkval);
}v is a local variable, so its address is near the current stack position. __brkval points near the current end of the heap. If the heap has not been used, the helper uses __heap_start instead.
Print measurements during operation
The following complete sketch prints free SRAM every second:
extern int __heap_start;
extern int *__brkval;
int freeRam() {
int v;
return (int)&v -
(__brkval == 0 ? (int)&__heap_start : (int)__brkval);
}
unsigned long lastReport = 0;
void setup() {
Serial.begin(9600);
Serial.println(F("Starting"));
Serial.print(F("Free RAM: "));
Serial.println(freeRam());
}
void loop() {
if (millis() - lastReport >= 1000) {
lastReport = millis();
Serial.print(F("Free RAM: "));
Serial.println(freeRam());
}
}The F() macro keeps these message literals in flash on AVR boards. Without it, the literals can consume SRAM.
A measurement is only a snapshot. If a function creates a large local array, free RAM can become smaller while that function runs and return to a larger value afterward. To observe that temporary use, measure inside the function:
void inspectBuffer() {
char buffer[64];
Serial.print(F("Inside function: "));
Serial.println(freeRam());
buffer[0] = '\0';
}Interpret the result carefully
A decreasing value often indicates that the heap is growing. This can happen when a String repeatedly changes size or when code allocates memory without releasing it. A low value that appears only during a function call may indicate excessive stack use instead.
| Observation | Possible cause |
|---|---|
| Value stays nearly constant | Mostly fixed-size storage |
| Value decreases after repeated operations | Heap growth or fragmentation |
| Value drops only inside one function | Large local variables |
| Random resets or corrupted output | Heap and stack are colliding |
The helper does not prove that every future allocation will succeed. Heap fragmentation can leave enough total free space but no single contiguous block large enough for a request. Also, interrupt handlers and nested calls temporarily consume stack space.
Exercise
Exercise
A sketch reports 900 bytes of free RAM at startup, 700 bytes after one hour, and 400 bytes after two hours. What should you investigate first?
Show answer
Investigate repeated dynamic allocation, especially growing or temporary String objects and malloc calls. Measure after the suspected operation and replace it with fixed-size storage where practical.