Skip to content

Instantly share code, notes, and snippets.

@anecdata
Created April 22, 2019 20:34
Show Gist options
  • Save anecdata/8460a4bcc7e87432bd409c78c5f3448a to your computer and use it in GitHub Desktop.
Save anecdata/8460a4bcc7e87432bd409c78c5f3448a to your computer and use it in GitHub Desktop.
displayio memory loss
import board
import os
import gc
import time
import random
import displayio
from adafruit_display_shapes.rect import Rect
def overwrite_bitmap(xw, yh, gmin, gmax):
x = random.randrange(0, xw)
y = random.randrange(0, yh)
c = random.randrange(0x000000, 0x1000000)
w = random.randrange(1, 33)
h = w
rect = Rect(x, y, w, h, fill=c)
splash[random.randrange(gmin, gmax)] = rect
starttime = time.monotonic()
with open('/VERSIONS.txt') as f:
cp_lib_bundle = f.readline().strip()
SCREENWIDTH = 320
SCREENHEIGHT = 240
SPLASHMAX = 250
splash = displayio.Group(max_size=SPLASHMAX)
print("splash len:", end=" ")
print(len(splash), end=" ")
for i in range(0, SPLASHMAX):
splash.append(displayio.TileGrid(displayio.Bitmap(1, 1, 1),
pixel_shader=displayio.Palette(1)))
print(len(splash))
board.DISPLAY.show(splash)
loop = 0
snap = time.monotonic()
while True:
overwrite_bitmap(SCREENWIDTH, SCREENHEIGHT, 0, SPLASHMAX)
elapsed = time.monotonic() - snap
if (elapsed >= 60): # every minute or so
snap = time.monotonic()
print(os.uname()[4].split()[1] +
os.uname()[4].split()[2].replace('with', ''), end=" ") # machine
print(os.uname()[3], end=" ") # CircuitPython version
print("CPLIB=%s" % cp_lib_bundle, end=" ")
print("Loop=%0i" % loop, end=" ")
print("Up=%0.3fs" % time.monotonic(), end=" ")
print("Run=%0.3fs" % (time.monotonic() - starttime), end=" ")
print("Mem=%0ib" % gc.mem_free(), end=" ")
print("%s" % gc.collect(), end=" ")
print("Mem=%0ib" % gc.mem_free(), end=" ")
print()
loop += 1
@anecdata
Copy link
Author

anecdata commented Apr 22, 2019

Actually, updating to latest (03f9048 on 2019-04-19) from 4.0.0-beta7 seems to have slowed or stabilized the loss.

The initial drops in free memory could easily be attributable to the simple initial TileGrids getting replaced with the more complex main loop TileGrids. In 4.0.0-beta7, the loss would continue at a similar rate for hours until a MemoryException occurred, but the latest circuitpython 03f9048, with gc fixes included, seems to have stopped the memory loss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment