0

CircuitPython Beta5 - memory allocation failed
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CircuitPython Beta5 - memory allocation failed

by Tonygo on Fri Feb 14, 2020 11:01 am

I’ve got several Adafruit boards and after using the new
line1.anchor_point = (0.5, 0) # middle top
and
line1.anchored_position = (120, 85)
syntax with the Love Heart project with CPB and a TFT Gizmo. I wanted to try it on my PyPortal. This needs CircuitPython 5-beta5 as it is a new addition.

I updated all the required libraries to 20200214 and the animation script already installed continued to work with
Adafruit CircuitPython 4.1.2 on 2019-12-18; Adafruit PyPortal with samd51j20

When I updated to
Adafruit CircuitPython 5.0.0-beta.5 on 2020-02-05; Adafruit PyPortal with samd51j20
The script crashed on line 9 with:

MemoryError: memory allocation failed, allocating 36864 bytes.

Moving back to CP 4.1.2 allowed the script to work – although with the 5beta5 libraries.

There appears to be a problem with CP 5-beta5

I’ve included the script.

Code: Select all | TOGGLE FULL SIZE
# HackSpace Mag 22 page 81
import board
import displayio
from time import sleep
import otter_data
display = board.DISPLAY
group = displayio.Group()

otter_bitmap = displayio.Bitmap(384,96,18)
background_file = open("/images/background.bmp", "rb")
background_bitmap = displayio.OnDiskBitmap(background_file)
background_tile = displayio.TileGrid(background_bitmap, pixel_shader=displayio.ColorConverter())

#load the otter pixel data
counter_x = 0
for line in otter_data.otter_bmp_data:

    counter_y = 0
    for pixel in line:
        otter_bitmap[counter_y,counter_x] = pixel
        counter_y = counter_y + 1
    counter_x = counter_x + 1
#load the otter pallette data
otter_pallette = displayio.Palette(18)
counter_colour = 0
for colour in otter_data.otter_pallette_data:
    otter_pallette[counter_colour] = colour
    counter_colour = counter_colour + 1
    otter_pallette.make_transparent(0)
    otter_tile = displayio.TileGrid(otter_bitmap,pixel_shader=otter_pallette, width=1, height=1, tile_width=96, tile_height=96)

group.append(background_tile)
group.append(otter_tile)

counter = 0
direction = 1

otter_tile.y=144
while True:
    for image_num in range(4):
        counter = counter + (9 * direction)
        otter_tile[0] = image_num
        otter_tile.x = counter
        display.show(group)
        sleep(0.15)
    if counter > 320 or counter < -96:
        direction = direction * - 1
        otter_tile.flip_x = not otter_tile.flip_x


Has anybody got any ideas?

Tonygo
 
Posts: 67
Joined: Fri Apr 13, 2018 11:09 am

Re: CircuitPython Beta5 - memory allocation failed

by Tonygo on Sat Feb 15, 2020 6:28 am

I've been investigating by adding some garbage collection at the start of the script and removing unwanted files.

Code: Select all | TOGGLE FULL SIZE
# HackSpace Mag 22 page 81
import board
import gc
import displayio
from time import sleep
import otter_data
display = board.DISPLAY
group = displayio.Group()
gc.collect()  # Make room
print(gc.mem_free())
otter_bitmap = displayio.Bitmap(384,96,18)
background_file = open("/images/background.bmp", "rb")
background_bitmap = displayio.OnDiskBitmap(background_file)
background_tile = displayio.TileGrid(background_bitmap, pixel_shader=displayio.ColorConverter())


Trying to run with CP4.1.2 I get 71952 bytes free but CP5-Beta5 I only have 32016bytes. Will the final version shrink a bit?

Tonygo
 
Posts: 67
Joined: Fri Apr 13, 2018 11:09 am

Re: CircuitPython Beta5 - memory allocation failed

by tannewt on Tue Feb 18, 2020 6:48 pm

That is a pretty dramatic difference between versions! Would you mind sharing all of the files needed to run the demo in a zip? I'd love to see what otter_data includes. Thanks!

tannewt
 
Posts: 1490
Joined: Thu Oct 06, 2016 8:48 pm

Re: CircuitPython Beta5 - memory allocation failed

by Tonygo on Wed Feb 19, 2020 7:28 am

I was really surprised to see how much bigger CircuitPython 5 had grown.

Here are the necessary files:

I did not write it - I got it from HackSpace Magazine #22 page 81.
There is a missing character in the printed code. My code.py is corrected.
You can get it here:

https://hackspace.raspberrypi.org/issues/22


Great article but I would like a tutorial from Adafruit on how to create background files and sprite files suitable for displayio with Paint or Gimp.
Attachments
OtterFiles.zip
(65.31 KiB) Downloaded 5 times

Tonygo
 
Posts: 67
Joined: Fri Apr 13, 2018 11:09 am

Re: CircuitPython Beta5 - memory allocation failed

by tannewt on Wed Feb 19, 2020 2:48 pm

Thanks Tonygo! I've filed an issue to look into this further before we make 5.0.0 stable: https://github.com/adafruit/circuitpython/issues/2639

I've got some BLE work to wrap up and then I'll take a look at this if no one else has.

tannewt
 
Posts: 1490
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.