Free RAM on MicroPython boards

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Free RAM on MicroPython boards

Post by rcolistete » Wed Apr 06, 2016 12:02 am

[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.
Last edited by rcolistete on Sat Feb 09, 2019 4:28 pm, edited 20 times in total.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Free RAM on MicroPython boards

Post by dhylands » Wed Apr 06, 2016 4:51 am

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.

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Free RAM on MicroPython boards

Post by rcolistete » Wed Apr 06, 2016 1:13 pm

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).
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Free RAM on MicroPython boards

Post by pythoncoder » Thu Apr 07, 2016 6:15 am

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.
Peter Hinch
Index to my micropython libraries.

MCHobby
Posts: 52
Joined: Mon Jan 26, 2015 2:05 pm
Contact:

Re: Free RAM on MicroPython boards

Post by MCHobby » Sun Apr 10, 2016 10:44 am

[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.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Free RAM on MicroPython boards

Post by dhylands » Sun Apr 10, 2016 7:45 pm

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

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Free RAM on MicroPython boards

Post by rcolistete » Wed Apr 20, 2016 5:33 pm

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.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Free RAM on MicroPython boards

Post by rcolistete » Wed May 04, 2016 11:09 am

My mistake, WiPy still has 56KB of free RAM for MicroPython 1.7 & 1.8.

Updated the 1st post.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Free RAM on MicroPython boards

Post by rcolistete » Wed Sep 14, 2016 6:03 am

Added free RAM for BBC Micro:bit, about 8.8 KB as I tested.
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Free RAM on MicroPython boards

Post by Roberthh » Wed Sep 14, 2016 6:47 am

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.

Post Reply