Page 1 of 4

Free RAM on MicroPython boards

Posted: Wed Apr 06, 2016 12:02 am
by rcolistete
[Moderators note: please note that the figures are valid on the date stated, and may vary from one MicroPython version to another.]

I need to know how much RAM (heap size ?) is available for the user in MicroPython running on :
- Pyboard v1.1 (99 KB < 192KB), MicroPython v1.8.6 (updated in 21/11/2016);
- Pyboard Lite v1.0 (84 KB < 128KB), MicroPython v1.8.6 (updated in 21/11/2016);
- Pyboard D SF2W (175 KB < ?KB) and SF2W (470 KB < ?KB), MicroPython 1.10 (updated in 09/02/2019);
- WiPy 1.0 (51 KB < 256 KB), firmware 1.3.0 MicroPython v1.8.2-103 (updated in 02/11/2016);
- ESP8266 (28.6 KB < 96 KB), MicroPython v1.8.6-7 (updated in 12/11/2016);
- BBC Micro:bit (8.4 KB < 16 KB), MicroPython v1.7-9 (used by the BBC Micro:bit online Python editor) (updated in 02/11/2016);
- LoPy (77 KB < 416 KB), firmware v0.9.6.b1 (updated in 27/11/2016);
- WiPy 2.0 (77 KB < 416 KB), firmware v0.9.6.b1 (updated in 27/11/2016);
- Espruino Pico (53 KB < 9 6KB), MicroPython v1.8.4 (updated in 02/11/2016);
- Teensy 3.1 (50 KB < 64 KB), MicroPython v1.8.6 dev-build from dhylands in 20/11/2016 (updated in 21/11/2016);
- Teensy 3.5 (166 KB < 192 KB), MicroPython v1.8.6 dev-build from dhylands in 20/11/2016 (updated in 21/11/2016);
- Teensy 3.6 (229 KB < 256 KB), MicroPython v1.8.6 dev-build from dhylands in 20/11/2016 (updated in 21/11/2016);
Meaning : MicroPython free RAM < Physical RAM. The values off free RAM are approximate because depend on the MicroPython version.
To measure the free RAM, after a new session, type :

Code: Select all

import gc
gc.collect()
gc.mem_free()


The reason is simple : I'm porting some Python 3 modules to MicroPython, and how much RAM they need x how much RAM available in some MicroPython boards is important to estimate which boards will be able to run the MicroPython module.

Re: Free RAM on MicroPython boards

Posted: Wed Apr 06, 2016 4:51 am
by dhylands
You can run the following to determine the amount of free RAM (this works on the pyboard).

Code: Select all

>>> import gc
>>> gc.mem_free()
101376
You should probably do a gc.collect() first to free up as much memory as can be freed.

Re: Free RAM on MicroPython boards

Posted: Wed Apr 06, 2016 1:13 pm
by rcolistete
Thanks.

But if anyone who uses ESP8266, BBC Micro:bit and LoPy could measure the free RAM and fill the "?" above, it would be very useful to the community port effort (from Python 3 -> MicroPython).

Re: Free RAM on MicroPython boards

Posted: Thu Apr 07, 2016 6:15 am
by pythoncoder
rcolistete wrote:Thanks.

But if anyone who uses ESP8266, BBC Micro:bit and LoPy could measure the free RAM and fill the "?" above, it would be very useful to the community port effort (from Python 3 -> MicroPython).
This is only likely to be meaningful where MicroPython has an official stable release for that platform. For example my ESP8266 boards show about 23K - 22K when connected to a wlan. But the port is under heavy development, one of the aims of which is to increase heap size. So I'd regard the figure as fairly meaningless for the purpose of planning future development.

Re: Free RAM on MicroPython boards

Posted: Sun Apr 10, 2016 10:44 am
by MCHobby
[quote="dhylands"]You can run the following to determine the amount of free RAM (this works on the pyboard). [code]>>> import gc
>>> gc.mem_free()
101376
[/code] You should probably do a gc.collect() first to free up as much memory as can be freed.[/quote]

I did also find the gs.mem_alloc()
However, I do wonder how to estimate the memory allocation of a given object.

Re: Free RAM on MicroPython boards

Posted: Sun Apr 10, 2016 7:45 pm
by dhylands
Probably the simplest way to determine the size of a given object is to do something like the following:

Code: Select all

>>> import gc
>>> class Foo:
...     def __init__(self):
...         self.bar = "123456"
... 
>>> def test():
...     gc.collect()
...     before = gc.mem_free()
...     foo = Foo()
...     gc.collect()
...     after = gc.mem_free()
...     print("Foo object takes up", before - after, "bytes")
... 
>>> test()
Foo object takes up 80 bytes
You want to do the calculation inside a function so that you don't get any memory used by the parser/compiler (when it compiles your line of input).

80 bytes corresponds to 5 heap blocks, which is currently the smallest that a class instance with 1 member can be. I did some analysis over here:
https://github.com/micropython/micropython/issues/1760

Re: Free RAM on MicroPython boards

Posted: Wed Apr 20, 2016 5:33 pm
by rcolistete
Updated the 1st post with approx. free RAM for LoPy (200 KB) and ESP8266 (22 KB).

So there is only BBC Micro:bit with unknown free RAM.

Re: Free RAM on MicroPython boards

Posted: Wed May 04, 2016 11:09 am
by rcolistete
My mistake, WiPy still has 56KB of free RAM for MicroPython 1.7 & 1.8.

Updated the 1st post.

Re: Free RAM on MicroPython boards

Posted: Wed Sep 14, 2016 6:03 am
by rcolistete
Added free RAM for BBC Micro:bit, about 8.8 KB as I tested.

Re: Free RAM on MicroPython boards

Posted: Wed Sep 14, 2016 6:47 am
by Roberthh
The maximum size for a script also depends on how the script is loaded. I give an example:

I have a 700 line python scripty. Compiling and loading it on a board requires about 70k of RAM space. If I pre-compiel it using mpy-cross, the size is about 15kBytes. In order to run that, about 20kBytes of RAM is sufficient (just start it, no data handling). If I precompile it and pack it into flash, which is supported by Pyboard and ESP8266, and maybe others too, the RAM usage is about 2k kBytes (again, just to start it). I do no know whether the BBC:Micro port supports .mpy file execution or frozen bytecode.