Interrupt/Call-Back delayed every 1.3s due to Garbage Collection #19676
Replies: 6 comments 5 replies
|
Maybe if I mess with these.... https://github.com/micropython/micropython/blob/master/py/mpconfig.h#L250 |
|
I do not know if you can you run the callback with |
|
If you call One idea would be to disable GC. This should ensure that Failing that you might look at optimisation: reducing allocations, speeding all code that is triggered by the interrupt... |
Probably the best thing you can do here is rewrite these parts of your code to not allocate as frequently. If you're using the micropython-lib usb-midi-device then this part shouldn't allocate too much, although there might be some allocation inside the library that's not obvious. Are you able to post the code you're using? The other thing you can do instead (or as well as) manual calls to |
|
Actually, it seems that the frequency at which gc is called doesn't matter that much. In #!/micropython
# vim: fileencoding=utf-8: ts=4: sw=4: expandtab:
# Standard RP2040 @126MHz
# MicroPython v1.28.0 on 2026-04-06; Raspberry Pi Pico with RP2040
import gc
from time import ticks_us, ticks_diff
def gc_timing1():
min_ = 9e9
max_ = 0
sum_ = 0
for n in range(1000):
free1 = gc.mem_free()
t0 = ticks_us()
gc.collect()
t1 = ticks_us()
free2 = gc.mem_free()
td = ticks_diff(t1,t0)
min_,max_ = min(min_,td),max(max_,td)
sum_ += td
print(f'Freed:{free2-free1:5} B in {td:6} µs ({min_} |{round(sum_/(n+1))}| {max_})')
def gc_timing2():
CHUNK = 100 # in bytes
KEEP = 500 # max list length
min_ = 9e9
max_ = 0
sum_ = 0
z = [bytearray(CHUNK) for _ in range(KEEP)] # prefill
for n in range(1000):
z.pop(0)
z.append(bytearray(CHUNK))
free1 = gc.mem_free()
t0 = ticks_us()
gc.collect()
t1 = ticks_us()
free2 = gc.mem_free()
td = ticks_diff(t1,t0)
min_,max_ = min(min_,td),max(max_,td)
sum_ += td
print(f'Freed:{free2-free1:5} B in {td:6} µs ({min_} |{round(sum_/(n+1))}| {max_})')
# gc_timing1() # ~480 µs
gc_timing2() # ~12000 µs / ~10000µs swapped |



That makes sense, if there's a lot of memory allocated then the gc scan will always take some time.
There are some early stage efforts to mark buffers which are pure data so that they can be skipped by the gc scan pass, see #19367