0

Premature MemoryError??
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Premature MemoryError??

by engthis on Tue Aug 03, 2021 10:06 pm

I am running circuitpython 6.3.0 on an arduino nano 33 IOT. Below is my code without a memory error
Code: Select all | TOGGLE FULL SIZE
import board
import busio as io
import adafruit_ssd1306
import adafruit_framebuf


i2c = io.I2C(board.SCL, board.SDA)
oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
oled.fill(0)
oled.text("T", 0, 0, 2, size=1)
oled.vline(67, 0, 32, 1)
oled.text("H", 72, 0, 2, size=1)
oled.show()


Here is my code that gives a memory error

Code: Select all | TOGGLE FULL SIZE
import busio as io
import adafruit_ssd1306
import adafruit_framebuf
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20

i2c = io.I2C(board.SCL, board.SDA)
owbus = OneWireBus(board.D8)
oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
oled.fill(0)
oled.text("T", 0, 0, 2, size=1)
oled.vline(67, 0, 32, 1)
oled.text("H", 72, 0, 2, size=1)

oled.show()


I have 33kb of 47kb used on the board if there is 254kb of sram how am I running out of memory already? Am I doing somthing wrong ? Any advice or suggestion welcome. Let me know if there is anything I add to make this post cleared or more concise.

engthis
 
Posts: 3
Joined: Tue Aug 03, 2021 9:59 pm

Re: Premature MemoryError??

by tannewt on Wed Aug 04, 2021 11:21 am

Please post the full memory error traceback. That'll key into the line of code that is failing.

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

Re: Premature MemoryError??

by engthis on Sat Aug 07, 2021 9:28 pm

Thanks for your response and sorry for the delay on my part. I managed to brick my board the day you responded and only managed to get it working again a few minutes ago.
Here is the output in the serial monitor when I run the code.

code.py output:
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "code.py", line 4, in <module>
  File "adafruit_onewire/bus.py", line 57, in <module>
MemoryError: memory allocation failed, allocating 80 bytes

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.

engthis
 
Posts: 3
Joined: Tue Aug 03, 2021 9:59 pm

Re: Premature MemoryError??

by tannewt on Mon Aug 09, 2021 4:57 pm

That board is a SAMD21 with 32k of RAM. Library imports take RAM so it seems plausible that you are just trying to load too much code.

The numbers you are seeing for flash. 256k flash total and then 64k for the drive with user code.

There isn't a lot to do here. Generally, boards with more RAM will allow you to use more libraries and have larger code.pys.

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

Re: Premature MemoryError??

by engthis on Thu Aug 12, 2021 8:13 pm

seems you are correct as this works on a teensy 4.0 no problem. Im even able to use my lcd screen at 128x64 resolution instead of downscaling to 128x32 to avoid a a different memory error. It just seems like a small amount of code to me. Would just using the default c++ use less memory?

engthis
 
Posts: 3
Joined: Tue Aug 03, 2021 9:59 pm

Re: Premature MemoryError??

by tannewt on Fri Aug 13, 2021 1:40 pm

The code.py is small but the code that the libraries that import aren't as small. `gc.mem_free()` will tell you how much free memory space is available.

C++ is definitely more memory efficient because you flash the code into flash instead of RAM. In CircuitPython, all user code is in RAM because it is dynamically loaded.

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

Please be positive and constructive with your questions and comments.