Definition
The static footprint of a program is the memory its built image occupies or reserves before it runs: code and constants in flash memory §6.6, and RAM regions whose sizes are fixed at build time, such as global variables, stack areas, a heap area and buffer pools. The dynamic footprint is the memory the program actually uses while running: how deep each stack grows, how much heap is allocated at once, how many pool buffers are taken. The static footprint shows reservations; the dynamic footprint needs measurement or analysis.
The idea
A microcontroller §6.1 has 64 KiB of RAM, where one KiB (kibibyte) is 1024 bytes, and the build reports that the program uses 50 KiB of it. Of those, 32 KiB is a heap area for allocation at run time, and a test run shows that the program never has more than 9 KiB of the heap allocated at once: the 50 KiB counts reservations at full size, filled or not. In the other direction, one thread’s stack area is 2 KiB, and an input that triggers an unusually deep chain of function calls needs 2.5 KiB. The build still reports that everything fits, and the program overflows its stack on that input. The build report sees neither fact, because both depend on what the program does when it runs.
A floor plan is a fair picture: the static footprint gives the size of every room, and the dynamic footprint how full each room gets in use. The picture stops at overflow: people cannot spill through a wall, but a stack that outgrows its area silently overwrites the memory next to it.
On a general-purpose operating system the gap is wider still. The operating system creates a program’s heap and its thread stacks when the program starts, so the build’s records show neither.
How it works
Static footprint, read from the build. The linker §12.4, the tool that joins compiled files into one program image and gives every piece an address, groups code and data into sections and places each section in a memory region named in the linker script §12.5, the file that describes the target’s memory.
.text: machine code, usually with constant data (.rodata); placed in flash..data: global variables with nonzero initial values. The initial values are stored in flash and copied to RAM at start-up, so.dataoccupies both..bss: global variables that start at zero; RAM only.
On a microcontroller, the stack areas and the heap area are usually further fixed RAM regions. The linker map, a text file the linker can write, lists every section and symbol with its address and size. The size command of common compiler toolchains §12.2 prints the totals text, data and bss: flash use is text plus data, and statically reserved RAM is data plus bss.
In the Zephyr real-time operating system (RTOS) §6.33, the rom_report and ram_report build targets list the size of every symbol placed in flash or in RAM. The system heap, from which k_malloc allocates, is one array §14.5 sized by the configuration option CONFIG_HEAP_MEM_POOL_SIZE, and the RAM report counts the whole array whether or not anything is ever allocated from it. Thread stacks defined at build time, and buffer pools such as the net_buf pools of Zephyr’s networking and Bluetooth code, also appear at full size.
A buffer pool is a fixed number of buffers of bytes each, reserved at build time: about bytes of RAM plus a small record per buffer, in use or not. When all buffers are taken, a request for another either waits until one is released or fails at once, depending on the timeout §9.34 the caller passes. For incoming data, a failed request means that the data is dropped, or that flow control holds the sender back until a buffer is free, which is called backpressure §9.30. The size also caps what one buffer holds, so a larger message must be split across buffers or rejected.
Dynamic footprint, measured or bounded.
Heap: the allocator’s statistics, the bytes allocated now and the largest number ever allocated at once, called the high-water mark. In Zephyr, the option
CONFIG_SYS_HEAP_RUNTIME_STATSmakes the heap keep these numbers.Stacks: stack painting. A new thread’s stack area is filled with a known byte pattern; later, scanning from the end toward which the stack grows, the first byte that no longer holds the pattern marks the deepest point reached. Zephyr’s thread analyzer reports this for each thread.
Pools: the number of buffers in use and the smallest number ever free.
Analysis: a compiler option §12.3 such as
-fstack-usagereports the stack frame size of each function. The deepest path through the call graph, the record of which function calls which, plus what interrupt handlers §6.12 can add on top, bounds the stack depth without running the program. This static analysis §3.1 holds only if there is no recursion and every call through a function pointer is accounted for.
Worked example
A hypothetical microcontroller has 256 KiB of flash memory and 64 KiB of RAM. The build reports:
| Item | Size |
.text (code and constants) |
40 KiB |
.data (initialized globals) |
1 KiB |
.bss (zeroed globals) |
5 KiB |
| heap area | 32 KiB |
| buffer pool, 8 buffers of 1 KiB | 8 KiB |
| two thread stacks of 2 KiB each | 4 KiB |
Flash use is KiB. Statically reserved RAM is KiB, which is bytes.
A test run measures the dynamic footprint: at most 9 KiB of the heap allocated at once; at least 5 of the 8 buffers always free, so at most 3 KiB of the pool in use; and stack high-water marks of 1.5 KiB and 0.75 KiB. The global variables always occupy their KiB. Adding the separate maxima gives an upper bound on the memory in use at any one moment of the run: KiB, which is bytes. Of the 50 KiB reserved, KiB was never observed in use.
Three readings of these numbers are wrong.
“29.75 KiB is free.” It is not free for other uses: it belongs to the heap, the pool and the stacks, and only they can use it.
“The program needs KiB.” This counts the heap twice: the 9 KiB allocated lies inside the 32 KiB heap area that the 50 KiB already includes.
“The first thread’s stack is safe, with 0.5 KiB to spare.” The 1.5 KiB is the deepest point the test reached. If a static analysis of that thread finds a call path that needs 2.5 KiB, the thread can overflow its 2 KiB area on an input the test did not try.
Stated precisely
Let the build reserve RAM regions with sizes bytes, fixed at link time. The static footprint in RAM is Let be the number of bytes of region in use at time . If the program never writes outside its regions, which excludes stack overflow §4.10 and heap corruption, then at every moment, and the dynamic peak in RAM satisfies . A test observes only some executions, so the peak it measures, , satisfies . Measurement therefore gives a lower bound on the true peak; is an upper bound only under the no-overflow condition, and a tighter upper bound needs analysis.
The linker map establishes and every . It does not establish , , or the condition on every execution path; for a stack region, that condition is exactly the question whether the stack can overflow. Memory that the program obtains at run time from outside its image, such as the heap and the stacks that a general-purpose operating system creates, belongs to no , so does not include it.
Code in flash does not change size at run time, so for code the static figure is complete. Data that the program writes to flash at run time, such as logs or settings, is a separate dynamic quantity.
Limits and common mistakes
File size is not footprint. An ELF §12.6 (Executable and Linking Format) file, the usual output of the linker, also holds symbol tables §12.8 and debugging information §12.9 that never reach the device. A file in the Intel hexadecimal object file format §12.7 (Intel HEX) writes each byte as two hexadecimal characters plus record overhead, so it is more than twice the size of the flash content.
Reserved is not used, and used is not needed: a measured high-water mark covers only the executions tested.
Moving memory between categories changes the static figure without changing the need. Turning a 4 KiB global buffer into a heap allocation lowers .bss by 4 KiB, but the heap must now have 4 KiB to spare at the moment of that allocation.
Comparing two builds needs the same categories, the same tool and the same unit. The unit kilobyte (KB) means 1000 bytes in some tools and 1024 bytes in others.
Inside the heap, allocator headers, alignment §4.22 padding and heap fragmentation §4.9 mean that fewer bytes than the heap size can be allocated in practice.
Questions answered
Does a linker map include all heap and stack demand?
No. A linker map, the file in which the linker lists every section and symbol of a built program with its address and size, shows what the build reserves, not what the program uses while it runs. On a microcontroller it shows the full size of each stack area and of the heap area, but not how deep each stack actually grows, how much of the heap is allocated at the busiest moment, or whether some input makes a stack overflow its area. On a general-purpose operating system it usually shows no heap or stack at all, because the operating system creates them when the program starts.
To learn the actual demand, measure it while the program runs: the allocator’s largest number of bytes allocated at once; stack painting, which fills each stack with a known pattern and later finds the deepest byte overwritten; and the smallest number of free buffers in each pool. Where a guarantee is needed, bound the demand by analysis, for example the stack frame sizes along the deepest call path plus what interrupt handlers can add. Report the reservation, the observed use and the analyzed bound as three separate numbers.
Related entries: Peak versus retained memory (§11.37), Instrumentation coverage (§14.35), Static, stack, and heap storage (§4.11), Stack overflow (§4.10), Linker (§12.4), Linker script (§12.5), Zephyr RTOS (§6.33).