How to measure memory usage in no_std environment?

I am new to embedded rust and bare metal programming in general, and I was wondering how I should go about determining how much memory my program is using? For reference, I am running my programs on a raspberry pi pico w (rp2040) and using embassy.

For pure Rust side of things, there's

that can track all allocations made by Rust. However, I'm not sure how that looks like on such a low end like rp2040. If you're not using std/alloc, then you'd have to instrument whatever else you're using instead. If you have 16K of RAM, then you may want to have a fixed memory layout.

First, define "memory usage."

In an environment with a supervisor, the supervisor (usually the operating system) manages sharing memory between multiple programs, and keeps track of which memory regions have been assigned to which programs at any given time. That makes "usage" both meaningful and relatively easy to measure: ask the supervisor.

In an embedded environment, there's frequently no supervisor. The only thing running on the computer is your program. One - entirely reasonable - answer to how much memory your program is using is "all of it," because there's no memory left for anything else to use, nor anything else running that could use it.

However, that's probably not entirely useful; one reason you might care about usage within your program is so that you can estimate how much more you can add to your program, or how large of a problem it can solve, given the constraints of the system it's installed on. There isn't really a one-size-fits-all answer to this (in general; embedded systems make the question more pressing but don't change the answer). You have to work out what your program uses for its stack, plus how your program manages the remainder of memory, and measure against that.

Some allocators do this for you. cap, which @kornel linked above, is one such example. You can also write your own allocator or look for one that is both tailored for the target system and tracks these kinds of stats for you.

1 Like

I happened to search for no_std allocators earlier and found one that does have allocation stats: talc. However, the docs don't seem be built with the counters featured enabled. Here is the source for counters.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.