Electronic – How to we determine memory usage of a microcontroller

compilerembeddedmemorymicrocontrollerstack

How can we determine how much memory is being used, or will be used by firmware in a microcontroller? What tools can support this?

Generally we don't use dynamic memory in embedded systems, but how can we determine the worst case stack usage for example? A compiler can't be enough.

For example we may not know until run-time how many times a function could be called, potentially causing a stack-overflow.

can a debugger tell us exactly at any point in the execution flow, how much memory is being used?

Best Answer

Yes, a debugger can tell you at any point in time how much memory is being used by the stack. Just check the current value of the stack pointer.

Another thing you can try is to fill up all of the unused RAM area with some obvious bogus value, like 0xAAAA. Run your code for a while and then check the RAM to see how many of the bogus values were overwritten...that's your maximum stack size.

The amount of memory used by static variables should be shown in some kind of log or report from the linker, as this is determined at compile time.