At what time should I manually run gc.collect()?


  • Pybytes Beta

    Should I setup a background thread to just run it every few hundred milliseconds? Is that what the automatic collector does?
    Is that reliable?
    https://docs.pycom.io/pycom_esp32/library/gc.html


  • Pybytes Beta

    So it sounds like one risk is latency. What other risks are there from running it?



  • @BetterAuto In one project I shut off automatic garbage collection and then called collect after each loop of measuring time with an ultrasound distance detector, to secure (as well as possible) that the timing wouldn't go wonky.



  • @BetterAuto

    Any reason not to just have it running in the background every few hundred milliseconds?

    then it can run in your time critical point and brake your logic...
    better have control when and where in the code it run.


  • Pybytes Beta

    Any reason not to just have it running in the background every few hundred milliseconds? It can check mem_free and auto-run. I can fork off a new thread and have it constantly run. Is that what the automatic collector does?



  • @BetterAuto
    There is not good answer to this question.
    As always it depends.

    But if you have infinite "main" loop
    then it is good practice if you put gc.collect() at least once.
    I say depends, because if you have some intensive memory operation
    you probably need to run it more the times.

    You can also monitor gc.mem_free() and after mem go to some level run gc.collect().

    You also can go in oposite direction and do not run it at all. It "should" be run automatically but then it can run in your time critical point and brake your logic.


Log in to reply
 

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.