ESP32 w/oled - mem issue?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
ebolisa
Posts: 4
Joined: Thu Feb 21, 2019 11:43 am

ESP32 w/oled - mem issue?

Post by ebolisa » Thu Feb 21, 2019 12:00 pm

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

OutoftheBOTS_
Posts: 574
Joined: Mon Nov 20, 2017 10:18 am

Re: ESP32 w/oled - mem issue?

Post by OutoftheBOTS_ » Thu Feb 21, 2019 8:04 pm

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

User avatar
ebolisa
Posts: 4
Joined: Thu Feb 21, 2019 11:43 am

Re: ESP32 w/oled - mem issue?

Post by ebolisa » Fri Feb 22, 2019 8:42 am

Thanks. Adding gc.collect() in the loop solves the problem but not sure it's the way to go.

User avatar
Roberthh
Posts: 1398
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ESP32 w/oled - mem issue?

Post by Roberthh » Fri Feb 22, 2019 9:00 am

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.

Plumedore
Posts: 1
Joined: Thu Feb 21, 2019 10:13 am

Re: ESP32 w/oled - mem issue?

Post by Plumedore » Fri Feb 22, 2019 12:22 pm

Does this process work well? What would be an alternative to something like that?

OutoftheBOTS_
Posts: 574
Joined: Mon Nov 20, 2017 10:18 am

Re: ESP32 w/oled - mem issue?

Post by OutoftheBOTS_ » Sat Feb 23, 2019 9:12 am

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.

Post Reply