RP2040 Swap chucks of code in/out of memory

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

RP2040 Swap chucks of code in/out of memory

Post by blakebr »

Hello,

Is there a way to unload chunks of CP 8.0.0 code like a .dll from a microcontroller like the RP2040 because of limited memory?

My solution would be to use a FRAM memory chip for all variables. A library that is called by all code chunks that defines the location in the FRAM where the variables can be found and

Code: Select all

supervisor.set_next_code_file(CODE)
supervisor.reload() # Ctrl-C then Ctrl-D
to switch between chunks.

I think if you were to look up UGLY in the CircuitPython dictionary this method would be listed as #1.

Bruce

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by mikeysklar »

Have you tried:

Code: Select all

del sys.module[‘modulename’]

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by blakebr »

Mike,

No I have not! Emoji with joy and surprise. I didn't know it existed. Thank you.

So I assume:

Code: Select all

define variables
import module_one
# module_one runs and does all its work.
del sys.module(module_one)
import module_two
# module_two runs and does all its work.
del sys.module(module_two)
etc.
Bruce

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by mikeysklar »

Yes, that is the idea at least.

The syntax will be slightly different and I’m not sure that memory will be cleaned out entirely just because the module is dropped.

The syntax really does use array brackets not function parenthesis.

Code: Select all

 sys.modules[module_name]
The memory clearing you will probably need to monitor with gc.mem_free() before loading the module, after loading the module and after deleting the module.

One final wrench I’d like to add is this is meant for deleting system modules (frozen and built-in) will it work for optional modules from the lib/ folder? Slightly unsure emoji with sweat beads.

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by blakebr »

Mike,

Sorry for the slow reply (not sorry). My new Canon MF264dw II printer arrived and I just HAD to set it up. I hope you understand. Emoji {Evil Grin}

No joy in Mudville. This is the code I am using.

Code: Select all

gc.collect()
print(gc.mem_free())
import ir_adafruit
gc.collect()
print(gc.mem_free())
# module_one runs and does all its work.
del sys.module[ir_adafruit]
gc.collect()
print(gc.mem_free())
time.sleep(100)
This is what REPL has to say for itself.
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
NTP_Clock_code.py output:
35552
34656
Traceback (most recent call last):
File "NTP_Clock_code.py", line 169, in <module>
AttributeError: 'module' object has no attribute 'module'

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.
The del sys. line is where the error occurs.

Bruce

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by mikeysklar »

A new printer is an exciting detour.

Trying the plural version.

Code: Select all

del sys.modules[ir_adafruit]
https://docs.circuitpython.org/en/lates ... ys.modules

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by blakebr »

Mike,

It is not crashing any more. I'll take that. This is the REPL output.

Code: Select all

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
NTP_Clock_code.py output:
35456
34560
34560
It doesn't give the memory back. I will try other stuff and get back to you.

Bruce

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by mikeysklar »

Okay, slight variation based on this thread @ stackoverflow.

Code: Select all

del sys.modules[ir_adafruit]
del ir_adafruit
gc.mem_alloc()
gc.mem_free()

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by blakebr »

Mike,

I had to add single quotes to the module name and gc.collect, then it worked. Here is the final code.

Code: Select all

del sys.modules['ir_adafruit'] # Quotes added.
del ir_adafruit
gc.mem_alloc()
gc.mem_free() # Probably not needed
gc.collect() # Also added
print(gc.mem_free())
time.sleep(10)
Here is the output to REPL.

Code: Select all

NTP_Clock_code.py output:
35440
34544
35312
Thank you,
Bruce

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by mikeysklar »

so no effect in actually freeing up memory is what I'm seeing? Just a bunch of command successfully running.

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by blakebr »

Mike,

No? I see a loss of 896 bytes with the load of the library. A recovery of 768 bytes with the code above. (I didn't show all the code where the library was loaded.) 128 bytes are still unrecovered. I assume a more complex library will show a better recovery amount.

Code: Select all

gc.collect(); print(gc.mem_free())
import ir_adafruit
gc.collect(); print(gc.mem_free())
# module_one runs and does all its work.
del sys.modules['ir_adafruit']
del ir_adafruit
gc.mem_alloc()
gc.mem_free()
gc.collect(); print(gc.mem_free())
time.sleep(1)
print(sys.modules)
Bruce

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by mikeysklar »

Cool. It worked.

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: RP2040 Swap chucks of code in/out of memory

Post by blakebr »

Yup!

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”