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

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
User avatar
shazz
Posts: 46
Joined: Tue Apr 30, 2019 6:35 pm
Contact:

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

Post by shazz » Mon May 20, 2019 12:41 am

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
8bits should be enough...

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

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

Post by jickster » Mon May 20, 2019 12:43 am

Do you mean at compile time of the C firmware?


Sent from my iPhone using Tapatalk Pro

User avatar
jimmo
Posts: 404
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

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

Post by jimmo » Mon May 20, 2019 5:25 am

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.

User avatar
shazz
Posts: 46
Joined: Tue Apr 30, 2019 6:35 pm
Contact:

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

Post by shazz » Mon May 20, 2019 11:28 am

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.
8bits should be enough...

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

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

Post by jickster » Tue May 21, 2019 3:26 am

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

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

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

Post by jickster » Tue May 21, 2019 3:32 am

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

Post Reply