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?
RAM usage of an object
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: RAM usage of an object
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.
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.
Index to my micropython libraries.
Re: RAM usage of an object
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.
Re: RAM usage of an object
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.
Re: RAM usage of an object
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.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.
Re: RAM usage of an object
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.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.
Re: RAM usage of an object
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/
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/