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.

2 Likes