micropython / micropython/micropython

gc.collect() does not work properly

Open
#11,698 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs-info port-esp32
Dominant language
C
Stars
22.1k
Forks
9k
Avg merge
6d 4h
Merged PRs (30d)
16

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided loop on an ESP32S3 with MicroPython v1.20.0 and recording gc.mem_free() across iterations. Compare the memory behavior around requests.get(), response.content, I2S.write(), and gc.collect(); done means identifying the cause of the declining free RAM and confirming a fix or documented limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.