adafruit / Adafruit_CircuitPython_PyPortal

CircuitPython driver for Adafruit PyPortal.
MIT License
45 stars 56 forks source link

Reduced chunk_size to reduce memory fragmentation #65

Closed cogliano closed 4 years ago

cogliano commented 4 years ago

Running a PyPortal Titano on CP 5.0.0-beta.4, I was getting MemoryError exceptions after 10 to 20 fetch() calls within a loop. Further research showed the memory was not low, but apparently fragmented. I added some gc.collect() calls in different functions, but that did not help. Finally, I reduced the chunk_size value from 12000 to 4096. This helped greatly, and I was able to get over 300 fetch loops (and counting) without the exception. I suggest implementing this solution until a better solution is found to address memory fragmentation with a higher higher chunk_size value.

The code used to test this issue is here:

https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/PyPortal_CMA_Art_Frame

You can reduce the while loop wait to 60 seconds to help speed up the looping for testing.

ladyada commented 4 years ago

did image download time increase significantly?

cogliano commented 4 years ago

Surprisingly, the download time decreased by about 2 seconds, perhaps due to less garbage collection in the loop? Here are my results using the Cleveland Museum learning guide code downloading the same image 3 times with the different chunk sizes:

chunk_size = 12000:

Created file of 296082 bytes in 12.6 seconds Created file of 296082 bytes in 12.7 seconds Created file of 296082 bytes in 12.7 seconds

chunk_size = 4096:

Created file of 296082 bytes in 10.6 seconds Created file of 296082 bytes in 10.4 seconds Created file of 296082 bytes in 10.5 seconds