A nice way to see exact memory usage

When building an embedded target with PIO, you normally get a brief summary of memory usage, e.g.

RAM:   [          ]   0.0% (used 28 bytes from 65536 bytes)
Flash: [          ]   0.2% (used 424 bytes from 262144 bytes)

But there’s another nice overview built into the linker, when adding -Wl,--print-memory-usage to build_flags:

Memory region         Used Size  Region Size  %age Used
             RAM:          28 B        48 KB      0.06%
            RAM2:          0 GB        16 KB      0.00%
           FLASH:         500 B       256 KB      0.19%

What I find useful, is that it lists all the regions defined in the .ld linker script, and as you can see in this example, there are in fact two separate RAM regions (on STM32L432KC, in this example). Another benefit, is that sometimes PIO’s .json description files are not 100% up to date, whereas this summary always reflects what the linker is doing.

For more tips, see this web page:

Update: the difference between 424 and 500 bytes flash “usage” is because PIO fails to account for the space needed for the initial interrupt vector as well as for the extra data in flash which gets copied to RAM on startup. IOW: the linker value is more complete.

3 Likes

In my case it displays slightly different numbers than the “standard” usage report:

Memory region         Used Size  Region Size  %age Used
             RAM:       12232 B        20 KB     59.73%
           FLASH:       51908 B        64 KB     79.21%
Checking size .pio/build/stm32/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=====     ]  52.2% (used 10696 bytes from 20480 bytes)
Flash: [========  ]  80.0% (used 51624 bytes from 64512 bytes)

It’s release configuration for STM32F103

Yep: 12232-10696 = 1536, which is space reserved in RAM for the two stacks (it’s slack, not actually used). And 51908-51624=284, because your µC probably has 284/4=71 entries in its vector table. The two reports base their values on different assumptions.

1 Like