Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

saving memory on Pico and other boards
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

saving memory on Pico and other boards

by MikeB35 on Thu Oct 14, 2021 3:20 am

Hello,
Is it possible to remove a circuitpython module from RAM that has been imported?
Example : 'import neopixel'
and as soon as you don't need the module any more in the code do 'del neopixel'.

I did a test and checked with 'gc.mem_free()', but after 'del module_name', the amount of memory available is even lower.

I am not sure if I have made myself clear.
Can you help me?

MikeB35
 
Posts: 60
Joined: Thu Feb 05, 2015 10:18 am

Re: saving memory on Pico and other boards

by mikeysklar on Fri Oct 15, 2021 6:17 pm

MikeB35,

The cleanest option would be not to use a public global variable when accessing the modules. Keep them in custom functions so that when you exit the function the memory is freed up.

Alternatively if you want to explore the road not taken something along the lines of:

Code: Select all | TOGGLE FULL SIZE
del sys.modules[module_name]


should delete the module, but it might be too messy for gc to relize the gains nor is it recommended.

mikeysklar
 
Posts: 6231
Joined: Mon Aug 01, 2016 8:10 pm

Re: saving memory on Pico and other boards

by MikeB35 on Sat Oct 16, 2021 9:25 am

mikeysklar wrote:MikeB35,

The cleanest option would be not to use a public global variable when accessing the modules. Keep them in custom functions so that when you exit the function the memory is freed up.

Alternatively if you want to explore the road not taken something along the lines of:

Code: Select all | TOGGLE FULL SIZE
del sys.modules[module_name]


should delete the module, but it might be too messy for gc to relize the gains nor is it recommended.



Thanks a lot for your answer. I will follow your advice.

Mike

MikeB35
 
Posts: 60
Joined: Thu Feb 05, 2015 10:18 am

Please be positive and constructive with your questions and comments.