Sub topic 2
How a Sketch Uses SRAM: Static Data, Heap and Stack
Assume you can write and upload a basic Arduino sketch, use the Serial Monitor, and understand C/C++ variables, arrays, functions, and pointers. This chapter uses an AVR-based board such as the Arduino Uno.
SRAM is the Arduino's working memory. The processor uses it while the sketch runs. SRAM holds variables, temporary values, text, and dynamically allocated objects. Unlike flash, SRAM loses its contents when power is removed.
The SRAM layout
A running sketch usually divides SRAM into three active areas:
- Static data contains global variables, variables declared with
static, and initialized or zero-filled data that exists for the entire program. - The heap contains memory requested at run time with functions such as
malloc()or by objects such asString. - The stack contains function parameters, local variables, return information, and interrupt-related data.
Static data starts at the lower part of SRAM. The heap grows upward as the program requests memory. The stack starts near the top and grows downward when functions are called.
The free space between the heap and stack is available for temporary use. If the two areas meet, one can overwrite the other. The resulting behavior is usually unpredictable rather than a clean error.
Static data, heap, and stack in code
char message[] = "Ready"; // Static data: exists throughout the sketch
int readings[20]; // Static data: 40 bytes on an Uno
void printReading(int value) {
char line[16]; // Stack: created on each call
snprintf(line, sizeof(line), "Value: %d", value);
Serial.println(line);
}
void setup() {
Serial.begin(9600);
printReading(42);
}
void loop() {
}message and readings are global, so they consume SRAM before setup() begins. The line array is local. It consumes stack space while printReading() runs and is released when the function returns.
A heap allocation has a different lifetime. For example, malloc(20) reserves space until the program calls free() or resets. String objects may allocate and release heap blocks as their contents change.
Why the areas collide
A large global array leaves less room for the heap and stack. A deeply nested sequence of function calls or a large local array pushes the stack downward. Repeatedly growing and shrinking heap objects can leave small unused gaps called fragments. The total unused memory might look sufficient, but no single gap may be large enough for the next allocation.
Interrupt handlers also use stack space. Leave room for them when estimating the maximum stack use.
You can estimate the current gap on AVR boards with this helper:
extern int __heap_start;
extern int* __brkval;
int freeRam() {
int local;
return (int)&local -
(__brkval == 0 ? (int)&__heap_start : (int)__brkval);
}
void setup() {
Serial.begin(9600);
Serial.print(F("Free SRAM: "));
Serial.println(freeRam());
}
void loop() {
}The result is an estimate, not a permanent guarantee. A function with a large local array can reduce free SRAM only while it runs. Heap allocations can also change the value later.
Exercise
Where does a local array live while its function is running, and where does a global array live?
Show answer
The local array normally lives on the stack. The global array lives in static data for the entire program.