Skip to content

MemoryError can occur when there is still lots of free memory #10451

Open
@FoamyGuy

Description

@FoamyGuy

CircuitPython version and board name

Adafruit CircuitPython 10.0.0-alpha.7-6-ge3c5548344 on 2025-06-25; Adafruit Fruit Jam with rp2350b

Code/REPL

import gc

from displayio import Group, TileGrid, Palette
from terminalio import Terminal, FONT

from adafruit_bitmap_font import bitmap_font
import supervisor

# un-comment and the font load succeeds
#l = [1024] * 1024

main_group = Group()
display = supervisor.runtime.display
display.root_group = main_group

print(f"free: {gc.mem_free()}")
# font = FONT
font = bitmap_font.load_font("fonts/terminal.lvfontbin")
print("font loaded")
while True:
    pass

Behavior

When the code is run as-is it raises this memory error:

code.py output:
free: 8132080
Traceback (most recent call last):
  File "code.py", line 18, in <module>
  File "adafruit_bitmap_font/bitmap_font.py", line 71, in load_font
  File "adafruit_bitmap_font/lvfontbin.py", line 77, in __init__
MemoryError: memory allocation failed, allocating 28161 bytes

However if you un-comment the l = [1024] * 1024 line to use up some more memory before attempting to load the font then it succeeds.

Description

It feels like there is some internal boundary with the RAM that causes this error to get raised if you happen to land too close to it (or perhaps right on it?). But if you have differently sized and shaped stuff in RAM you can avoid this boundary and thus not get Memory Error. Maybe related to the PSRAM on the device?

Additional information

This is the font file used: https://github.com/adafruit/Fruit-Jam-OS/blob/main/fonts/en_US.lvfontbin

Though I am not sure if the specific font matters so much as what is loaded before it and how big it is, I assume any font found hit this "unlucky" boundary that causes this to occur under the right circumstances.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions