I haven't really investigated but it doesn't seem to matter much. My test program is -As is with no 'del()' of the 'time' import ...And, with the 'del()' ...And with an early import ...It may depend on whether you are importing a '.py' module, '.mpy' or something in-built to MicroPython as a frozen '.py' module or as a native C module. I would guess the more methods defined the more RAM will get used and I have never been sure what 'del()' actually does, what it will free-up.
I would say don't worry about it unless you encounter issues. If you prefer late binding I don't see a problem with that, nor in adding 'del()', can't see much harm in doing that even if it doesn't free much or anything.
Code:
import gcdef Report(desc): print("{:<32} {:,}".format(desc, gc.mem_free()))def Method(): Report("Before 'import time'") import time Report("After 'import time'") if False: del(time) Report("After 'del(time)'")if False: Report("Before 'import time'") import time Report("After 'import time'")else: Report("Before calling 'Method()") Method() Report("After return from 'Method()'")gc.collect()Report("After garbage collection")
Code:
Before calling 'Method() 149,984Before 'import time' 149,920After 'import time' 146,336After return from 'Method()' 146,272After garbage collection 148,064
Code:
Before calling 'Method() 149,952Before 'import time' 149,888After 'import time' 146,304After 'del(time)' 146,240After return from 'Method()' 146,176After garbage collection 148,000
Code:
Before 'import time' 149,904After 'import time' 146,320After garbage collection 148,176
I would say don't worry about it unless you encounter issues. If you prefer late binding I don't see a problem with that, nor in adding 'del()', can't see much harm in doing that even if it doesn't free much or anything.
Statistics: Posted by hippy — Sat Jan 27, 2024 5:12 pm