Memory Allocation failed

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
Quaddam
 
Posts: 52
Joined: Fri Nov 11, 2016 1:49 pm

Memory Allocation failed

Post by Quaddam »

Hi I am running the code below n a trinket M0, and am recieving this error, doesnt seem like a lot of code? Wondering what is taking up all the space?
Screenshot 2022-03-04 092509.jpg
Screenshot 2022-03-04 092509.jpg (48.57 KiB) Viewed 218 times

Code: Select all

import board
from neopixel import NeoPixel
from analogio import AnalogIn
from adafruit_led_animation.animation.chase import Chase
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.color import WHITE, PURPLE, ORANGE, JADE

pixels = NeoPixel(board.D2, 60, brightness=1, auto_write=True)
pot = AnalogIn(board.D3)

def show_colours(val):
    if(val > -1 and val <= 6):
        comet.animate()
    if(val > 6 and val < 12):
        comet1.animate()
    if(val > 12 and val < 18):
        chase.animate()
    if(val > 24 and val < 30):
        chase1.animate()
   
chase = Chase(pixels, speed=0.1, color=WHITE, size=3, spacing=6)
chase1 = Chase(pixels, speed=0.1, color=JADE, size=3, spacing=6)
comet = Comet(pixels, speed=0.01, color=PURPLE, tail_length=10, bounce=True)
comet1 = Comet(pixels, speed=0.01, color=ORANGE, tail_length=10, bounce=True)

while True:
    show_colours(int(pot.value/65520*30))
    print(int(pot.value/65520*30))


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

Re: Memory Allocation failed

Post by tannewt »

I believe the led animation libraries are pretty large.

To see how much each import takes, you can do `import gc` first and then `print(gc.mem_free())` after each import to see how much memory remains free.

User avatar
Quaddam
 
Posts: 52
Joined: Fri Nov 11, 2016 1:49 pm

Re: Memory Allocation failed

Post by Quaddam »

Thanks for the info, that works well, its displaying 6336 and 6272 for the memory after two animation modules are imported.
I slimed down my code to run just one animation library and it works well.
So if I run just import gc then print(gc.mem_free()) it shows me 20544, so this is the total memory on my board then?
So when I print the memory at the end of my code I am getting readings as in the pic below. Its quite the fluctation between 2880 and 80! So I am right on the edge of memory I assume. Is there any way to boost that memory so I could add more animation libraries, or is the solution just a larger memory board?

Code: Select all

import board
import time
import gc
from neopixel import NeoPixel
from analogio import AnalogIn
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.color import WHITE, PURPLE, ORANGE, JADE


pixels = NeoPixel(board.D2, 60, brightness=1, auto_write=True)
pot = AnalogIn(board.D3)

def show_colours(val):
    if(val > -1 and val <= 15):
        comet.animate()
    if(val > 15 and val < 50):
        comet1.animate()
    
comet = Comet(pixels, speed=0.01, color=PURPLE, tail_length=30, bounce=True)
comet1 = Comet(pixels, speed=0.01, color=ORANGE, tail_length=30, bounce=True)
print(gc.mem_free())
while True:
    show_colours(int(pot.value/65520*30))
    print(gc.mem_free())
    time.sleep(.5)
    
pyth.jpg
pyth.jpg (8.41 KiB) Viewed 206 times

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

Re: Memory Allocation failed

Post by tannewt »

Yup, the 20544 is the number of bytes free for the CircuitPython VM after setting aside RAM for static allocations and the stack. The SAMD21 is 32k total so you only have a bit more possible with tweaks.

Generally, yeah. Microcontrollers with more RAM are the easiest way to get more.

User avatar
Quaddam
 
Posts: 52
Joined: Fri Nov 11, 2016 1:49 pm

Re: Memory Allocation failed

Post by Quaddam »

Thanks for the help! I Appreciate it.

User avatar
danhalbert
 
Posts: 4652
Joined: Tue Aug 08, 2017 12:37 pm

Re: Memory Allocation failed

Post by danhalbert »

Here is some more info about running the animation library on SAMD21 boards:
https://learn.adafruit.com/circuitpytho ... -3074329-2

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

Return to “Adafruit CircuitPython”