saving memory on Pico and other boards

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
AmazingCircuitPythonFR35
 
Posts: 99
Joined: Thu Feb 05, 2015 10:18 am

saving memory on Pico and other boards

Post by AmazingCircuitPythonFR35 »

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?

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

Re: saving memory on Pico and other boards

Post by mikeysklar »

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

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.

User avatar
AmazingCircuitPythonFR35
 
Posts: 99
Joined: Thu Feb 05, 2015 10:18 am

Re: saving memory on Pico and other boards

Post by AmazingCircuitPythonFR35 »

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

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

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

Return to “Adafruit CircuitPython”