Use of gc over and over?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Use of gc over and over?

Post by Jibun no kage » Fri Jul 29, 2022 2:43 am

I was reviewing the following code...

Code: Select all

import gc
import usocket as socket
import ustruct as struct

gc.collect()
from ubinascii import hexlify
import uasyncio as asyncio

gc.collect()
from utime import ticks_ms, ticks_diff
from uerrno import EINPROGRESS, ETIMEDOUT

gc.collect()
from micropython import const
from machine import unique_id
import network

gc.collect()
from sys import platform
Initially this stuck me as a bit odd, given that modern heap management should optimize and deference heap pointers, but above code suggests that gc is not that flexible or otherwise limited. Is that really the case? Would not setting a gc threshold service as a method to encourage gc to do the work, versus calling it explicitly over and over as above? Or is it the case that the loading the various modules severely (can) fragment the heap, and gc can't handle it without explicit invocation? Just looking for a bit of confirmation or clarity on why the above was done, as it was done.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Use of gc over and over?

Post by pythoncoder » Fri Jul 29, 2022 7:59 am

When you import a module which exists as Python sourcecode the compiler converts it to bytecode. This process involves allocations and running an explicit gc.collect() reclaims the allocations. The aim is to reduce RAM fragmentation. This can be an issue if the application needs to allocate large buffers. That said, the example above does seem rather enthusiastic in its use.

Another reason to explicitly issue gc.collect() is that it runs faster if it doesn't have much to do. Some of my code periodically does this to ensure that the blocking period is short and occurs at times when it has least impact on the application.
Peter Hinch
Index to my micropython libraries.

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: Use of gc over and over?

Post by p_j » Fri Jul 29, 2022 9:40 am

pythoncoder wrote:
Fri Jul 29, 2022 7:59 am
When you import a module which exists as Python sourcecode the compiler converts it to bytecode. This process involves allocations and running an explicit gc.collect() reclaims the allocations. The aim is to reduce RAM fragmentation. This can be an issue if the application needs to allocate large buffers. That said, the example above does seem rather enthusiastic in its use.

Another reason to explicitly issue gc.collect() is that it runs faster if it doesn't have much to do. Some of my code periodically does this to ensure that the blocking period is short and occurs at times when it has least impact on the application.
If the code is pre-compiled to bytecode, does this remove the need to run gc.collect?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Use of gc over and over?

Post by Roberthh » Fri Jul 29, 2022 9:46 am

No, because Python uses a dynamic memory allocation approach. Objects are created and freed at runtime.

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Use of gc over and over?

Post by Jibun no kage » Sat Jul 30, 2022 3:46 pm

So... the long and short, my understanding was correct, explicit gc.collect forces consolidation, and the benefit of frequent invocation can improve over all responsive/speed of collect since it has less to do.

Does setting a gc threshold not help with this after initialization? I recall in my past ESP coding is I set the go threshold to a given value, which I determined by watching the heap during testing, would ensure that gc did collections based on need versus explicit invocation at various points in code. At the time this seemed more logical and elegant, and peppering collects in code. Is my perception of how the threshold feature still valid, IMHO, so, no?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Use of gc over and over?

Post by Roberthh » Sat Jul 30, 2022 4:15 pm

Setting a GC threshold helps to start the automatic GC collection earlier. And in my experience manual optimisation of the GC behaviour is only required at devices with small heaps, to avoid allocation fails, and those with very large heaps, if GC collection time matters. In any case it is better to allocate large buffers early and keep them.

Post Reply