Page 1 of 1

ESP32 w/oled - mem issue?

Posted: Thu Feb 21, 2019 12:00 pm
by ebolisa
Hi,

With the below code I noticed that the board's memory space is decreasing while looping. Shouldn't the memory space remain the same?

TIA

Code: Select all

import machine, ssd1306, gc
from machine import I2C

from time import sleep

import esp
esp.osdebug(None)

i2c = I2C(scl=machine.Pin(4), sda=machine.Pin(5))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

def display_txt():
    oled.fill(0)
    #oled.text('MicroPython on', 0, 0)
    #oled.text('an ESP32 with an', 0, 10)
    #oled.text('attached SSD1306', 0, 20)
    txt = 'free mem: {}'.format(gc.mem_free())
    oled.text(txt, 0, 0)
    oled.show()
   
try:
    while True:
        display_txt()
        sleep(0.05)
        #print('mem free: {}'.format(gc.mem_free()))
        
except OSError as e:
  print(e)
results:

mem free: 113184
mem free: 113024
mem free: 112864
mem free: 112704
mem free: 112544
.......
mem free: 464
mem free: 336
mem free: 208
mem free: 80
mem free: 116272
mem free: 116112
mem free: 115952

Re: ESP32 w/oled - mem issue?

Posted: Thu Feb 21, 2019 8:04 pm
by OutoftheBOTS_
I would make the assumption that during oled.text() it creates a buffer in which it places the text data to be sent to the oled but upon exit it is not del the buffer. I would try adding garbage collecion bewteen each call and see if during garbabge collection the buffer gets returned to the heap

Re: ESP32 w/oled - mem issue?

Posted: Fri Feb 22, 2019 8:42 am
by ebolisa
Thanks. Adding gc.collect() in the loop solves the problem but not sure it's the way to go.

Re: ESP32 w/oled - mem issue?

Posted: Fri Feb 22, 2019 9:00 am
by Roberthh
It was at no point a problem, but just showed how MicroPython's heap management works. if you create new objects (here: th etext to be shown and printed), the space for that will be taken form the heap, until eventually no space is available any more, at which time the automatic garbage collection runs. The call to gc() does the same.

Re: ESP32 w/oled - mem issue?

Posted: Sat Feb 23, 2019 9:12 am
by OutoftheBOTS_
I think you misunderstand Roberthh answer.

Basically when memory is allocated then there is less memory left on the heap. When that allocated memory is finished with it isn't returned to the heap until garbage collection is called. The automatic garbage collect will be called anytime the available heap becomes low. If you don't want to wait till the automatic garbage collection is called then you can call it your self, most people don't worry. The main time you may want to call manual garbage collection is when your doing time critical processes and you don't want to be delayed by automatic garbage collection at a time critical part in your program so you call it manually at more appropriate time in your program.