Out of memory due to heap fragmentation?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
roygbiv
Posts: 1
Joined: Tue Feb 14, 2017 11:24 am

Out of memory due to heap fragmentation?

Post by roygbiv » Tue Feb 14, 2017 12:02 pm

After only 43 loops my program runs out of memory, looking at the heap I can see it's fragmented and can't allocate what is needed. Is there anyway I can fix this?

Below is my script after reading forums and various blogs for the last few hours. I'm basically collecting the data from the DHT22 sensor and passing it to another site to store it in a db. The sleep function used to be longer but I shortened it to reproduce the wrror quicker.

I believe the fault lies within urequests, since running 43 urequests.post in a row will cause the same error.

I'm running esp8266-20170108-v1.8.7.bin and I have the ESP-01s

import gc
gc.enable()
gc.collect()
import machine
gc.collect()
import urequests
gc.collect()
import dht
gc.collect()
import time
gc.collect()

d = dht.DHT22(machine.Pin(2))
url = 'http://192.168.1.3/homenas/ourapi.php'
o = {'Content-Type': 'application/x-www-form-urlencoded'}

while True:
d.measure()
t = str(d.temperature())
h = str(d.humidity())
p = "a=sdfsdf&t=" + t + "&h=" + h
ourrequest = urequests.post(url, headers = o, data = p)
time.sleep(5)
gc.collect()

User avatar
Mike Teachman
Posts: 69
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: Out of memory due to heap fragmentation?

Post by Mike Teachman » Wed Feb 15, 2017 2:57 am

one minor suggestion is to dynamically concatenate strings in the loop with .format rather than "+". You garbage collect at the end of the loop so it's doubtful this will help, if at all...described here:

http://docs.micropython.org/en/latest/w ... ained.html

ad525
Posts: 9
Joined: Tue Oct 03, 2017 12:09 pm

Re: Out of memory due to heap fragmentation?

Post by ad525 » Tue Oct 03, 2017 12:13 pm

Hello roygbiv,
do you find the problem ? I have the same problem, I'm using urequest library, gc is enabled and after 10 hours of running the esp8266-01 freeze.

Any idea ?

User avatar
pythoncoder
Posts: 3509
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Out of memory due to heap fragmentation?

Post by pythoncoder » Wed Oct 04, 2017 3:01 pm

You can display the heap with

Code: Select all

import micropython
micropython.mem_info(1)
as discussed in the document referenced by Mike Teachman.
Peter Hinch

Post Reply