Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

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

    Scheduled Pinned Locked Moved MicroPython
    garbagecollecto
    6 Posts 3 Posters 1.9k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B Offline
      BetterAuto Pybytes Beta
      last edited by BetterAuto

      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

      L papasmurphP 2 Replies Last reply Reply Quote 0
      • L Offline
        livius @BetterAuto
        last edited by livius

        @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.

        think about people first, then computers

        1 Reply Last reply Reply Quote 1
        • B Offline
          BetterAuto Pybytes Beta
          last edited by BetterAuto

          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?

          L 1 Reply Last reply Reply Quote 0
          • L Offline
            livius @BetterAuto
            last edited by

            @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.

            think about people first, then computers

            1 Reply Last reply Reply Quote 1
            • papasmurphP Offline
              papasmurph @BetterAuto
              last edited by

              @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.

              1 Reply Last reply Reply Quote 1
              • B Offline
                BetterAuto Pybytes Beta
                last edited by

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

                1 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post

                Pycom on Twitter

                Pycom