Page 1 of 1

Find out how much RAM will be used by MicroPython at compile the runtime

Posted: Mon May 20, 2019 12:41 am
by shazz
Hi,
Sorry if this topic was already discussed... did not find it while searching the forums.

Before I start coding some useless tools, is there a way to:
- at compile time to show the RAM and Flash that will be allocated by the core and each module (kind of size mod....o ?)
- at runtime to monitor dynamic memory allocation (m_malloc) by module

Thanks!

shazz

Re: Find out how much RAM will be used by MicroPython at compile the runtime

Posted: Mon May 20, 2019 12:43 am
by jickster
Do you mean at compile time of the C firmware?


Sent from my iPhone using Tapatalk Pro

Re: Find out how much RAM will be used by MicroPython at compile the runtime

Posted: Mon May 20, 2019 5:25 am
by jimmo
shazz wrote:
Mon May 20, 2019 12:41 am
- at compile time to show the RAM and Flash that will be allocated by the core and each module (kind of size mod....o ?)
By core I assume you mean MicroPython core (i.e. /py/*.c), not sure what you mean by module, but perhaps you mean a single translation unit (i.e. one .c file?). You can look at the size of each symbol in the corresponding object file.
e.g.

Code: Select all

nm --print-size --radix=d ports/stm32/build-PYBV11/modmachine.o
Because of linker optimisations etc it's a bit hard to map this exactly to what ends up in the final firmware. (i.e. an unused function will be dropped).

However you can use the same trick on the final firmware.elf file to look at the sizes of functions.
shazz wrote:
Mon May 20, 2019 12:41 am
- at runtime to monitor dynamic memory allocation (m_malloc) by module
I'm not aware of a way to do this, but haven't looked sorry.

Re: Find out how much RAM will be used by MicroPython at compile the runtime

Posted: Mon May 20, 2019 11:28 am
by shazz
Thanks,
Yes, exactly. I meant
- core: py/*.c
- modules: extmod/*.c => so that in the board definition I can select/remove modules based on my RAM needs

I will play with nm and add a little script to generate those stats on demand, at least to keep track of the memory (flash and RAM) usage.

Re: Find out how much RAM will be used by MicroPython at compile the runtime

Posted: Tue May 21, 2019 3:26 am
by jickster
The flash usage is trivial to discover: all the information is contained in the elf.

I think it’s easiest to transform the elf into a pure binary file and then take note of the size of said binary file.


Sent from my iPhone using Tapatalk Pro

Re: Find out how much RAM will be used by MicroPython at compile the runtime

Posted: Tue May 21, 2019 3:32 am
by jickster
You cannot discover the RAM usage statically like with flash usage.

RAM usage depends on the code ie how much memory it Allocates.
But in general you can’t deduce that info just by searching for “malloc” within the code.
The number of mallocs could be dynamic, based on an input.
There could be complicated logic that determines how much memory a module maintains.




Sent from my iPhone using Tapatalk Pro