Monitoring Memory Resources

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
smartrf
 
Posts: 19
Joined: Sat Sep 28, 2013 6:25 pm

Monitoring Memory Resources

Post by smartrf »

As i build my application targeted at the SAMD21 with CircuitPython, what are best practices for monitoring the use of the limited memory resources available to me ?
I have found the following:
import gc
gc.collect()
gc.mem_free()

I think this is providing me a value of available RAM or heap space. the number changes depending upon history of commands when issued thru the REPL So i guess that is why it is important to issue the gc.collect() before the gc.mem_free()

Is there a sequence of commands or a way to know the amount of flash memory that is being used by the program segments that are in flash ?

are there any best practices to minimize RAM usage or flash usage ?
If at application requires a lot of strings ( like for example, communicating to a serial device with an AT like command set ) is there a best way to allocate the strings so that they use a minimum of memory resources. Certainly not repeating string segments over and over again, and instead use a string variable/constant and then build the desired string to send over the UART, probably should be considered.

I would just like to better understand how to conserve memory so that the device could support a reasonable size app.

any suggestions would be welcome.

User avatar
tannewt
 
Posts: 3314
Joined: Thu Oct 06, 2016 8:48 pm

Re: Monitoring Memory Resources

Post by tannewt »

Hi smartrf,
Thanks for the questions. mem_free() is the best way to monitor the allocations during runtime. collect() is useful as you've discovered because it will free memory thats no longer used.

To measure code size, you can create a separate module and do a collect/mem_free before and after the import.

Strings are treated specially by MicroPython (and therefore CircuitPython). On load, identical strings are deduplicated. So, you are right thinking that splitting unique strings into shared sub-strings will save memory. If the strings contain unique numbers, it may be best to use Python's string formatting to create the desired string.

To save flash space, we recommend using mpy-cross to generate smaller mpy files which are bytecode rather than plain text. mpy files also take less intermediate memory on load.

One last resource is the heap analysis tools I added a while back. It allows you to see the internal C locations that allocate memory and can give you a clue what type of objects in the Python code are expensive. Instructions are available for that here: https://github.com/adafruit/circuitpyth ... ctivity.md

Good luck!
~Scott

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

Return to “Adafruit CircuitPython”