Mem Allocation Fails on Itsy Bitsy M0 Express

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
Einstein007
 
Posts: 13
Joined: Sun Mar 18, 2018 5:44 pm

Mem Allocation Fails on Itsy Bitsy M0 Express

Post by Einstein007 »

I am using the Itsy Bitsy M0 Express with a ds18x20 temp sensor and it works great. I also am using it with a 7 segment LED display which works well. When I run a program that combines the two, I get an error like this:
Traceback (most recent call last):
File "main.py", line 35, in <module>
File "adafruit_ht16k33/segments.py", line 195, in number
File "adafruit_ht16k33/segments.py", line 182, in text
MemoryError: memory allocation failed, allocating 160 bytes
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.

The display fails and the program stops running. The program may run 30 minutes, but it also may do this after 10 minutes.
This is my program - any help on what I can do to keep the display going as the program without using the display does for weeks?

Code: Select all

import time
import board
import busio as io
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20
import adafruit_ht16k33.segments
i2c = io.I2C(board.SCL, board.SDA)
display = adafruit_ht16k33.segments.Seg7x4(i2c)

# Initialize one-wire bus on board pin D10.
ow_bus = OneWireBus(board.D10)

# Scan for sensors and grab the first one found.
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])

j = 0
print('Count', 'Raw', 'C', 'F', sep='\t', end='\n')  
# Main loop to print the temperature every 5 minutes.
while True:
    j = j + 1
    temp_c = ds18.temperature
    temp_f = (temp_c * 9/5) + 32
   
    dt = round(ds18.temperature, 2)
    ct = round(temp_c, 2)
    ft = round(temp_f, 2)
    print(j, dt, ct, ft, sep='\t', end='\n')
    display.number(ct)
    display.show()
    time.sleep(5)
    display.fill(0)
    display.show()
    display.number(ft)
    display.show()
    
    time.sleep(293)
    display.fill(0)
    display.show()
Thank You for any help.
Last edited by adafruit_support_carter on Tue Mar 27, 2018 1:21 pm, edited 1 time in total.
Reason: added [code] tags

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by adafruit_support_mike »

It sounds like the code has a memory leak.. something is allocating memory but not releasing it.

I'm going to move this thread to the CircuitPython forum so the folks there can take a look at the code.

User avatar
Einstein007
 
Posts: 13
Joined: Sun Mar 18, 2018 5:44 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by Einstein007 »

Thank you.

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by adafruit_support_carter »

I've set this up and run your code and could not reproduce the error after ~1 hour of running. I also reduced your time.sleeps to 1 second each and ran that for a good 30 minutes and still could not reproduce it.

What version of CircuitPython and the libraries are you using?

setup for ref:
CP version = 2.2.4
lib bundle version = March 24, 2018 auto-release
setup.jpg
setup.jpg (225.28 KiB) Viewed 972 times

User avatar
Einstein007
 
Posts: 13
Joined: Sun Mar 18, 2018 5:44 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by Einstein007 »

Thank you very much for taking the time to recreate my entire set-up. I had an older library and an older CircuitPython. I am now using CP 2.2.4 and have updated my library to March 24, 2018. I see the new version of the library still does not round correctly when you display.number(4444) or others. I get 4443. The last digit is still wrong on some numbers. My programs running just the LED display work well. The programs that just print my temperatures to putty or MU (REPL) works well. I am still getting errors on the program I listed on the forum. I spend a couple of hours on this tonight without any change. Maybe it is my Itsy Bitsy board. I have another which I have not soldered the header pins on yet and also ordered 4 more which were supposed to have arrived today by UPS but have not. Their website says in transit and they have no more info. I am an amateur radio operator and built many projects including receivers and transmitters. I do have decent soldering skills. I built a COSMAC ELF back in the 70's with an RCA 1802 CMOS microprocessor. It was fun programming it with 8 switches to enter the HEX codes in machine language to program the binary equivalents. I will try another Itsy Bitsy and will check for soldering bridges with a strong magnifying glass on the current one I am running.

Once again I really appreciate you taking time to help me.

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by adafruit_support_carter »

I see the new version of the library still does not round correctly when you display.number(4444) or others. I get 4443.
Hmmm. Yah, I'm getting that to. Appears to be related to underlying precision in circuitpython.
I am still getting errors on the program I listed on the forum.
What errors are you getting? Doubt it's anything specific to just the one board. At least not something that can't be remedied.

User avatar
Einstein007
 
Posts: 13
Joined: Sun Mar 18, 2018 5:44 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by Einstein007 »

I am running the same program you proved works well. (What files should be in the library folder in thethe adafruit_ht16k33 folder?) I assume the new library has the files needed because the programs listed below work well. The errors are always about memory. It’s crazy – the current error message is this:
main.py output:
Traceback (most recent call last):
File "main.py", line 7, in <module>
MemoryError: memory allocation failed, allocating 768 bytes

Press any key to enter the REPL. Use CTRL-D to reload.
However, if I load the following two programs, they run fine without ANY error messages:

Code: Select all

#Example of a variable counting and putting it on LED display
#in count folder
import board
import busio as io
import adafruit_ht16k33.segments
import time
from time import sleep
i2c = io.I2C(board.SCL, board.SDA)
display = adafruit_ht16k33.segments.Seg7x4(i2c)

display.fill(0)
display.show()

for j in range(0, 9999):
    display.number(j)
    display.show()
    sleep(.1)
-------------------------------------------------------

Code: Select all

#demo of LED display commands
import board
import busio as io
import adafruit_ht16k33.segments
import time
from time import sleep
i2c = io.I2C(board.SCL, board.SDA)
display = adafruit_ht16k33.segments.Seg7x4(i2c)

for j in range(0, 500):
    display.fill(0)
    display.show()
    sleep(.5)
    display.put('1', 0)
    display.show()
    sleep(.5)
    display.put('2', 1)
    display.show()
    sleep(.5)
    display.put('3', 2)
    display.show()
    sleep(.5)
    display.put('4', 3)
    display.show()
    sleep(.5)
    display.fill(0)
    display.show()
    sleep(.5)
    display.put('5', 0)
    display.show()
    sleep(.5)
    display.put('6', 1)
    display.show()
    sleep(.5)
    display.put('7', 2)
    display.show()
    sleep(.5)
    display.put('8', 3)
    display.show()
    sleep(.5)
    display.fill(0)
    display.show()
    sleep(.5)
    display.number(9990)
    display.show()
    sleep(.5)
    display.number(8889)
    display.show()
    sleep(.5)
    display.number(7778)
    display.show()
    sleep(.5)
    display.number(6667)
    display.show()
    sleep(.5)
    display.number(5556)
    display.show()
    sleep(.5)
    display.number(4444)
    display.show()
    sleep(.5)
    display.number(3333)
    display.show()
    sleep(.5)
    display.number(2222)
    display.show()
    sleep(.5)
    display.number(1112)
    display.show()
    sleep(.5)
    display.number(0000)
    display.show()
    sleep(.5)
    display.hex(0xABCD)
    display.show()
    sleep(1)
    display.hex(0xEF00)
    display.show()
    sleep(1)
    display.hex(0xFADE)
    display.show()
    sleep(1)
    display.hex(0xFACE)
    display.show()
    sleep(1)
    display.hex(0xFEED)
    display.show()
    sleep(1)
    display.number(0000)
    display.show()
    sleep(.5)
    display.fill(0)
    display.show()
    display.number(0000)
    display.show()
    sleep(.2)
    display.fill(0)
    display.show()
    display.number(0000)
    display.show()
    sleep(.2)
    display.fill(0)
    display.show()
    display.number(0000)
    display.show()
    sleep(.2)
    display.fill(0)
    display.show()
    display.number(0000)
    display.show()
    sleep(.2)
    display.fill(0)
    display.show()
    display.number(0000)
    display.show()
    sleep(.2)
    display.fill(0)
    display.show()
    display.number(0000)
    display.show()
    sleep(.2)
    display.fill(0)
    display.show()
    display.number(0000)
    display.show()
    sleep(.2)
    display.fill(0)
    display.show()
    display.number(0000)
    display.show()
    sleep(.2)
Last edited by adafruit_support_carter on Wed Mar 28, 2018 12:58 pm, edited 2 times in total.
Reason: added [code] tags

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by adafruit_support_carter »

FYI, the 4443 thing is a bug:
https://github.com/micropython/micropython/issues/1450
It is being looked at for getting fixed in CircuitPython.
What files should be in the library folder in thethe adafruit_ht16k33 folder?
should be same as here:
https://github.com/adafruit/Adafruit_Ci ... it_ht16k33
I am running the same program you proved works well.
So, your original code in the first post? If so it sounds like you have updated your itsy bitsy to latest CP (2.2.4) and put the latest libs on. But you still get that MemoryError?

User avatar
Einstein007
 
Posts: 13
Joined: Sun Mar 18, 2018 5:44 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by Einstein007 »

yes, you are correct. I am trying to run the code I posted in the original post. The same one you ran without a problem. I did update to the latest library and have the current version of CP. I did not install the new library as directed. I just moved the old one from the Itsy Bitsy MO Express and put it on my desktop. I then copied the new library from my computer to the Itsy Bitsy. I followed the direction on installing the new CP by hitting the reset button twice and copying the latest CP from my computer. The controller comes up with the correct version (new one) on the REPL screen.
Thanks

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by adafruit_support_carter »

What operating system / computer are you using to program the Itsy Bitsy? And what text editor?

User avatar
Einstein007
 
Posts: 13
Joined: Sun Mar 18, 2018 5:44 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by Einstein007 »

I am using a Dell desktop running Windows 7 professional, 64 bit OS. I use MU to write and edit the code. I save the code out to my hard drive and overwrite the current main.py by copying the file to the Itsy Bitsy and replacing it with the new main.py. It works with every other file without a problem. Using the MU REPL or Putty to see the return data via the USB cable when not using the LED 7 segment display. USB cable is made for syncing and supports data.
Thanks

User avatar
sommersoft
 
Posts: 20
Joined: Sun Aug 14, 2016 10:43 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by sommersoft »

Einstein007,

Trying to apply another set of eyes on this; usually a good thing (not always though :D).

For this error, I think it's related to the libraries (is there a # comment above the first import in your code? that would make line 7 the segment import):

Code: Select all

main.py output:
Traceback (most recent call last):
File "main.py", line 7, in <module>
MemoryError: memory allocation failed, allocating 768 bytes

Press any key to enter the REPL. Use CTRL-D to reload.
Regarding the DS18x20, OneWire, & HT16K33 libraries, are you using the .mpy versions? Your error message says .py; the .py versions of those 3 libraries total around 20+ KBs in raw format. The .mpy versions will be smaller pre-bytecoded files, and also keep the firmware from having to use RAM space to convert them to bytecode. The .mpy libraries for CircuitPython 2.2.4 are here (most recent build): https://github.com/adafruit/Adafruit_Ci ... 180328.zip

An additional tip with importing libraries, is to import the larger ones first. Has to do with reserving larger blocks of data on the heap, before the blocks run out. This is the order I would try and import in:

Code: Select all

import adafruit_ht16k33.segments
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20
import busio as io
import time
import board
As for what is presenting itself as a possible memory leak, going to have to debug a little further if it continues...

User avatar
Einstein007
 
Posts: 13
Joined: Sun Mar 18, 2018 5:44 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by Einstein007 »

Sommersoft, you are a life saver! Yes, I had a comment on line 1 at the top of the program but I deleted it when I put it on the forum. It just told me what folder the main.py was located since all the programs have the same name. I had both the ,py libraries and the .mpy but I had no idea what the difference was. I am learning a lot from you and others which are so nice to take time to help me. I changed the one wire and the 7 seg display libraries to .mpy, and re-arranged the order of the import of libraries just as you suggested. The program is humming along without any memory problems. I have the C and F temps on the display for 3 seconds each and loop again, while printing the info to the computer via MU and REPL. For awhile, I was starting to think it would never work. A big question is why the .py and .mpy libraries? Should I just replace the other library folders with the .mpy versions?

I plan to let this run all night and see if I end up with a memory problem again. I'll let you know what happens. I feel like I owe you a case of beer or what ever you enjoy. I have 5 other Itsy Bitzy's, 5 different displays, and other sensors to play with. It looks like I'll have some fun and learn. It is so nice Adafruit runs these forums. They are number 1 in my book. There are many talented people like you willing to share their knowledge with people like me. Thank you so much!

User avatar
sommersoft
 
Posts: 20
Joined: Sun Aug 14, 2016 10:43 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by sommersoft »

Should I just replace the other library folders with the .mpy versions?
I would say it is a "best practice" to use the .mpy versions, but it's really only required when you start running into memory issues related to imports. The main reason I noticed that was likely your issue: I've experienced it first-hand. :D
A big question is why the .py and .mpy libraries?
Well, that's a longer topic that I have scant authority to explain properly. However, I can expound on it a little bit.
  • The .py files are human-readable; it's their original format and allows for changes to be made if desired. I would say that is a major reason Adafruit continues to make them available. EDIT: another reason is that the documentation for ReadTheDocs "lives" in the libraries as they're hosted on GitHub. You can also read the documentation in your local copies.

    The .mpy files are not totally human-readable: they have all whitespace and comments removed. Furthermore, they are converted into bytecode. For more on that, research how Python works as an interpreted language, compared to say C/C++ which are compiled languages. There is a really good podcast explaining this as well (interview of the project lead for CircuitPython). Here is the link (Python explanation starts at 14:25): https://theamphour.com/383-an-interview ... shawcroft/
I feel like I owe you a case of beer or what ever you enjoy.
We'll call it a virtual beer, and I appreciate it. It's enjoyment enough helping people out. I too have received plenty from Adafruit's forums, learn guides, etc...when I can pay it back, I try to.

Please do let us know how tonight's test works out. And, if it doesn't work quite right, we'll be here to help.

User avatar
Einstein007
 
Posts: 13
Joined: Sun Mar 18, 2018 5:44 pm

Re: Mem Allocation Fails on Itsy Bitsy M0 Express

Post by Einstein007 »

Sommersoft, thank you again. The program is still running after 4,239 loops. After I sent the post, I thought about the .py vs .mpy and came up with the same concept you told me. I figured if people wanted to modify the .py libraries themselves, they would be able to do it. It makes sense to have both. I just checked the Itsy and the properties shows 1.22 Mb used, 767 Kb free space for a total of 1.97 Mb of memory. Have a great day and holiday weekend. I really appreciate your help.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”