How to Calculate Flash Memory and RAM Usage#
Flash#
Flash is a type of non-volatile memory that retains stored data even when the power is turned off.
Usage: Stores firmware (the program code), configuration data, and constant data, such as the interrupt vector table, which doesn't change frequently.
Random Access Memory (RAM)#
RAM is a type of volatile memory that holds data only while power is on. When the device is turned off, all data in RAM is lost.
Usage: Temporarily stores data during program execution. This includes data in variables, programs stacks, buffers, and so on.
Differences between Flash and RAM#
Feature | Flash | RAM |
|---|---|---|
Volatility | Non-volatile (retains data without power) | Volatile (loses data when power is off) |
Speed | Slower read/write | Faster read/write |
Usage | Stores firmware and constants | Stores variables and temporary data |
Write Durability | Limited write cycles | Unlimited write cycles |
Memory sections#
Programs typically use the following memory sections during startup and runtime.
Code / Program / .text#
Purpose: Stores the actual executable code or instructions that the processor runs.
Typical location: Flash (non-volatile storage)
Characteristics: This memory section is read-only after compilation. This section doesn’t change at runtime.
Read-Only Data (RO-data)#
Purpose: Holds constants or any data that should not change at runtime, such as lookup tables or string literals.
Typical location: Flash or Read-Only Memory (ROM)
Characteristics: This data is read-only and stored alongside code, since it’s non-modifiable.
Initialized Data (.data, RW-data)#
Purpose: Contains global and static variables that have initial values assigned in the code.
Typical Location: Copied from flash to RAM at startup
Characteristics: This memory section is read-write accessible at runtime. The initial values are loaded into RAM, and they can change throughout the program execution.
The .data section is included in both flash and RAM usage calculations because it has a dual role:
Flash (non-volatile storage): The .data section contains initialized global and static variables. These variables need to retain their initial values when the program starts. Therefore, the initial values for these variables are stored in flash when the program is compiled and flashed onto the microcontroller.
RAM (volatile storage): During program execution, these initialized variables from the .data section are copied from flash to RAM. This allows the variables to be read and modified during runtime.
Uninitialized Data (.bss, ZI-data)#
Purpose: Stores global and static variables that are declared but not assigned an initial value.
Typical Location: RAM
Characteristics: The startup code initializes variables to zero. This memory section is writable at runtime.
Stack#
Purpose: Temporarily holds function call information, local variables, and return addresses. The stack grows and shrinks as functions are called and returned.
Typical Location: RAM
Characteristics: The system manages the information during runtime. The stack is generally a contiguous block of memory, with growth limited by predefined bounds.
Heap#
Purpose: Allocates dynamic memory at runtime. For example, malloc and calloc.
Typical Location: RAM
Characteristics: This memory section is flexible in size as it’s allocated during runtime as needed. However, excessive heap use can lead to fragmentation.
Exception Index Table (.ARM.exidx)#
Purpose: Unwinds the stack during exceptions or error, specifically in ARM-based systems.
Typical Location: Flash or a specific section of memory defined by the ARM architecture
Characteristics: This memory section contains metadata for exception handling. It's primarily used in advanced debugging and error handling mechanisms.
Copy Table or Program Data Copy Table#
Purpose: Initializes sections in RAM during startup using values stored in flash, such as the .data section.
Typical Location: Flash
Characteristics: The copy table is a part of the startup code process. The startup code uses it to copy initial values from flash to their respective RAM locations.
.bss Section:#
Purpose: Holds global and static variables that are declared but not explicitly initialized in the code.
Characteristics: These variables are initialized to zero by the system at runtime.
The .bss section provides an efficient way to represent memory regions for uninitialized variables, as it doesn't need to store actual data in the program file—it only needs the size of the memory region.
Calculation of Flash and RAM for various IDEs#
IDE / Toolchain | Flash Usage | RAM Usage |
|---|---|---|
Simplicity Studio IDE | .vectors + .text + .ARM.exidx + .copy.table | .data + .stack + .bss + .heap |
Keil µVision | Code + RO-data | RW-data + ZI-data |
STM32CubeIDE | text + data | data + bss |
Atmel Studio | Program | Data |
IAR Embedded Workbench | RO size | RW size + ZI size |
SEGGER Embedded Studio | Code + RO-data | RW-data + ZI-data |
GNU GCC | text + data | data + bss |