← index #11698Issue #10718
Related · high · value 0.387
QUERY · ISSUE

gc.collect() does not work properly

openby StreakingJerryopened 2023-06-04updated 2024-07-13

I am running MicroPython v1.20.0 on 2023-04-26; ESP32S3 module (spiram octal) with ESP32S3

import network
from machine import Pin, I2S
import urequests as requests
import struct
import gc
from debugfun import breakpoint

gc.enable()

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if wlan.isconnected():
    wlan.disconnect()
wlan.connect('SSID', 'ssidssid')

sck_pin = Pin(12) 
ws_pin = Pin(13)
sdo_pin = Pin(14)

audio_out = I2S(0,
                sck=sck_pin, ws=ws_pin, sd=sdo_pin,
                mode=I2S.TX,
                bits=8,
                format=I2S.MONO,
                rate=44100,
                ibuf=8192)

buf_size = 4096
buf = memoryview(bytearray(buf_size))

while True:
    print("Free RAM:",gc.mem_free())
    text=input('>Say:\n')
    text = str(text).encode("utf-8")
    text = ''.join('{:02x}'.format(x) for x in text)    
    response = requests.get(url=("http://xxxxx/?d=" + text))
    data = response.content
    print("Data size:",len(data))
    i = data.find(b"data")+8
    while i < len(data):
        buf = data[i:i+buf_size] 
        i += buf_size
        audio_out.write(buf)
    del text
    del response
    del data
    del i
    gc.collect()

The Free RAM get lower after every loop. I have deleted all variables created in the loop so I think it is supposed to be a constant free memory.

CANDIDATE · ISSUE

Memory allocation remains after an exchange with the REPL via the serial port.

closedby jczicopened 2023-02-11updated 2023-02-15
bug

Hi, there is something strange about the memory reservation through the REPL via UART.

  • MicroPython v1.19.1 on 2022-06-18
  • ESP32C3 module
  • IDF v4.4.1 with new lib 3.3.0

To reproduce my problem, you just have to connect serially to the REPL, copy/paste a lot of characters (either in the normal prompt, in RAW mode or copy/paste mode), for example a size of 15000, and then look at the memory allocated afterwards via gc.mem_alloc().

These characters seem to remain somewhere, even after a gc.collect().
Sometimes disconnecting the serial port and then reconnecting it empties this memory reservation, but more often than not, it remains and cannot be freed.
Also, a call to micropython.mem_info(1) show a real difference.

Is this a problem on my part or on the side of Micropython or IDF?
Maybe related to the logic of fragmentation blocks... I have no idea where this comes from.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied