Section 11.38

Static versus dynamic footprint

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.

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 NN of buffers of SS bytes each, reserved at build time: about N×SN \times S bytes of RAM plus a small record per buffer, in use or not. When all NN 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 SS also caps what one buffer holds, so a larger message must be split across buffers or rejected.

Dynamic footprint, measured or bounded.

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 40+1=4140 + 1 = 41 KiB. Statically reserved RAM is 1+5+32+8+4=501 + 5 + 32 + 8 + 4 = 50 KiB, which is 50×1024=51,20050 \times 1024 = 51{,}200 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 1+5=61 + 5 = 6 KiB. Adding the separate maxima gives an upper bound on the memory in use at any one moment of the run: 6+9+3+1.5+0.75=20.256 + 9 + 3 + 1.5 + 0.75 = 20.25 KiB, which is 20.25×1024=20,73620.25 \times 1024 = 20{,}736 bytes. Of the 50 KiB reserved, 50−20.25=29.7550 - 20.25 = 29.75 KiB was never observed in use.

Three readings of these numbers are wrong.

Stated precisely

Let the build reserve RAM regions R1,…,RkR_1, \dots, R_k with sizes |R1|,…,|Rk||R_1|, \dots, |R_k| bytes, fixed at link time. The static footprint in RAM is S=∑j=1k|Rj|.S = \sum_{j=1}^{k} |R_j|. Let uj(t)u_j(t) be the number of bytes of region RjR_j in use at time tt. If the program never writes outside its regions, which excludes stack overflow §4.10 and heap corruption, then 0≤uj(t)≤|Rj|0 \le u_j(t) \le |R_j| at every moment, and the dynamic peak in RAM D=maxt∑j=1kuj(t)D = \max_t \sum_{j=1}^{k} u_j(t) satisfies D≤SD \le S. A test observes only some executions, so the peak it measures, DobsD_{\text{obs}}, satisfies Dobs≤DD_{\text{obs}} \le D. Measurement therefore gives a lower bound on the true peak; SS is an upper bound only under the no-overflow condition, and a tighter upper bound needs analysis.

The linker map establishes SS and every |Rj||R_j|. It does not establish uj(t)u_j(t), DD, or the condition uj(t)≤|Rj|u_j(t) \le |R_j| 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 RjR_j, so SS 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).