Memory: Is "from X import Y" better than "import X"?
Posted: Tue Jun 13, 2017 4:41 pm
I'm occasionally getting errors when my ESP boots a new version of main.py, like:
MemoryError: memory allocation failed, allocating 557 bytes
Which I think I understand to be running out of memory as the ESP tries to compile the main.py file.
I manage to find ways to reduce the code size or variable allocations, but I'm wondering if other things will help. Specifically, if I'm only using a few commands from an imported module, I assume the "from X import Y" approach means I'm only importing those particular sections, saving memory? Is that right?
My specific example:
from utime import time,sleep,localtime;from machine import reset,RTC,Pin
I also import usocket rather than socket. I understand the 'u' versions may be more limited, but if they do what I need I'm good?
The "import from" also makes the code a bit smaller, as I code
x=time() rather than x=utime.time()
and I have a lot of those commands. Byte-code wise, probably no difference, but I'm not compiling anything, just loading a text "main.py" file.
Is there anyway to get the Memory Allocation when it doesn't fail (so I can be proactive, and see when I get close)?
Any clarification is greatly appreciated. -NTL2009
MemoryError: memory allocation failed, allocating 557 bytes
Which I think I understand to be running out of memory as the ESP tries to compile the main.py file.
I manage to find ways to reduce the code size or variable allocations, but I'm wondering if other things will help. Specifically, if I'm only using a few commands from an imported module, I assume the "from X import Y" approach means I'm only importing those particular sections, saving memory? Is that right?
My specific example:
from utime import time,sleep,localtime;from machine import reset,RTC,Pin
I also import usocket rather than socket. I understand the 'u' versions may be more limited, but if they do what I need I'm good?
The "import from" also makes the code a bit smaller, as I code
x=time() rather than x=utime.time()
and I have a lot of those commands. Byte-code wise, probably no difference, but I'm not compiling anything, just loading a text "main.py" file.
Is there anyway to get the Memory Allocation when it doesn't fail (so I can be proactive, and see when I get close)?
Any clarification is greatly appreciated. -NTL2009