Garbage Collection takes a long time (15ms instead of 1ms) and I am not sure what is causing the memory leak in the first place #16231
Replies: 5 comments 15 replies
-
How long does your |
Beta Was this translation helpful? Give feedback.
-
Little bit of an update. I have found at least 1 leak. Is this expected? As simple as
vs
If you monitor free memory will see the second line consumes 16 bytes and its "Gone" |
Beta Was this translation helpful? Give feedback.
-
Not a Micropython memory expert, but a couple of things stand out:
So if my understanding of your situation is correct, I'd suggest:
Other comments which may help your mental model:
It may help diagnose your problem if you can give more detail about how your |
Beta Was this translation helpful? Give feedback.
-
Floating point operations always allocate. To avoid allocation use small (30-bit) ints. |
Beta Was this translation helpful? Give feedback.
-
Yes. This applies to allocations in general: they persist until the next GC. See this doc. The duration of |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I have built a robot using an RP2040 Connect running the latest version of MicroPython. I posted a separate thread about the architecture a while ago here.
The issue I am facing is that while my robot's control loop is running, it consumes a consistent amount of free RAM per cycle. This causes the garbage collector to kick in every 70 cycles, which is very consistent.
This isn't a problem in itself, except that the collection takes 15-16ms to complete. The documentation suggests that this should take around 1ms, which would be completely fine. If I force garbage collection, it still takes 15-16ms to run, so the time taken to execute is independent of the free RAM available.
I thought the issue might be caused by the local variables I am using within various methods inside the classes I have created. I went through and made all variables part of the class
__init__
function, so all variables within a class are referenced usingself
. However, after re-reading caching-object-references, it seems I shouldn't have everything at the class level.Also, after going through this exercise, it made absolutely no difference to how much RAM is being consumed per cycle.
This leaves me with at least two questions:
I am still unclear whether MicroPython passes by reference or by value. Perhaps forcing a pass-by-reference approach could improve memory usage.
Thanks,
Paul
Beta Was this translation helpful? Give feedback.
All reactions