RAM usage of an object

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ajie_dirgantara
Posts: 81
Joined: Fri Sep 02, 2016 9:26 am

RAM usage of an object

Post by ajie_dirgantara » Tue Aug 01, 2017 4:03 am

between following codes :

class testobj():
data1 = 0
data2 = 0
def__init__(self):
self.data1 = 1
self.data2 = 2
def changeData1(self, value):
self.data1 = value

o_test = testobj()
o_test.changeData1(10)

and directly call :

data1 = 0
data2 = 0
def changeData1(value):
global data1
data1 = value

changeData1(10)

I assume, correct me If I wrong, option two is more likely to consume less RAM?
And then my real question is,
if I instantiate an object, does the methods also allocate some memory in RAM and the code is called from RAM?

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

Re: RAM usage of an object

Post by pythoncoder » Tue Aug 01, 2017 7:31 am

Regarding the code samples I guess the use of globals might save a few bytes but I'd always use a class if it resulted in better, more maintainable code.

When MicroPython imports a module, and that module is a Python source file in the filesystem, the compiler is invoked. This produces bytecode, which is stored in RAM and executed by the MicroPython VM.

It is possible to cross-compile the code and import the bytecode directly from a .mpy file. This avoids the compilation phase, but the bytecode is still stored in RAM. To avoid storing the bytecode in RAM you can use frozen bytecode where you create a firmware build which puts the bytecode in flash. On STM boards such as the Pyboard the code is executed from flash.

I'm not sure whether this is true for other SOC architectures. AIUI the ESP8266 can't execute code from Flash and has to copy it to RAM. However bytecode isn't machine code - perhaps someone else can clarify this point.
Peter Hinch
Index to my micropython libraries.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: RAM usage of an object

Post by deshipu » Tue Aug 01, 2017 7:23 pm

Instances of objects (including classes) will be stored in RAM (because they are mutable). A class will store pointers to its methods as attributes -- so each method will add at least 4 bytes to the memory usage. The method code itself can be either in ram or in flash, depending on whether the program is pre-compiled or not, exactly the same as functions.

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

Re: RAM usage of an object

Post by Roberthh » Tue Aug 01, 2017 8:04 pm

ESP8266 and ESP32 flash is accessed via SPI. Thus nothing can be executed or used directly. It always has to be copied to RAM first. This method is quite common.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: RAM usage of an object

Post by deshipu » Tue Aug 01, 2017 10:44 pm

Roberthh wrote:ESP8266 and ESP32 flash is accessed via SPI. Thus nothing can be executed or used directly. It always has to be copied to RAM first. This method is quite common.
It's actually not entirely true. It's true that it's accessed via SPI, but there is a chunk of iRAM reserved as a cache for flash, and things can be run from flash using that, without using extra RAM.

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

Re: RAM usage of an object

Post by Roberthh » Wed Aug 02, 2017 5:26 am

It's actually not entirely true. It's true that it's accessed via SPI, but there is a chunk of iRAM reserved as a cache for flash, and things can be run from flash using that, without using extra RAM.
Sure, but even if it is transparent, it has to be copied into that cache RAM, causing a delay, which makes in average (virtual) execution form flash slower and a little bit unpredictable timing-wise.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: RAM usage of an object

Post by pfalcon » Wed Aug 02, 2017 7:47 am

It will be possible to find out RAM usage of an object once this patch is finished and merged: https://github.com/micropython/micropython/pull/3215
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply