Skip to content

Instantly share code, notes, and snippets.

@anecdata
Created April 22, 2019 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

Output:

PyPortal 03f9048 on 2019-04-19 CPLIB=20190420 Loop=148 Up=1207.128s Run=60.844s Mem=138384b None Mem=166560b 
PyPortal 03f9048 on 2019-04-19 CPLIB=20190420 Loop=256 Up=1270.421s Run=124.136s Mem=137984b None Mem=160864b 
PyPortal 03f9048 on 2019-04-19 CPLIB=20190420 Loop=355 Up=1333.749s Run=187.464s Mem=134208b None Mem=157872b 
PyPortal 03f9048 on 2019-04-19 CPLIB=20190420 Loop=456 Up=1397.387s Run=251.102s Mem=130384b None Mem=155392b 
PyPortal 03f9048 on 2019-04-19 CPLIB=20190420 Loop=557 Up=1460.033s Run=313.748s Mem=127776b None Mem=153568b 
PyPortal 03f9048 on 2019-04-19 CPLIB=20190420 Loop=659 Up=1520.567s Run=374.283s Mem=126320b None Mem=152976b 

@anecdata
Copy link
Author

Expected to crash with MemoryException in Rect, within a couple of hours. I'll update this comment with result.

@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