Skip to content

Reducing freeze time for gc.collect() #10456

Open
@jbrelwof

Description

@jbrelwof

gc.collect() freezes things for a sizeable chunk of time. How can that be improved?

  • faster measurement
    • optimization without good measurements is... challenging
    • gc.mem_alloc() and gc.mem_free() are terribly slow - they use gc_info() (in C) to add up stats for each block in each mp_state_mem_area_t
      • adding a singleton to track the same info would add a little overhead in time for allocating and collecting instances, but would dramatically improve the speed of gc.mem_alloc() and gc.mem_free()
      • half of the hooks (allocation side) to do this appear to already be in place for the memorymonitor module, adding some tracking for frees during collection shouldn't be too difficult
  • control when collection happens
    • gc.disable() and manually call gc.collect()
    • at least manages when the pause will be, but not how long it will be
    • would benefit greatly from faster measurement to determine when it needs to be done
  • control how long collection takes
    • this is probably the most important improvement for getting things under control
      • but might be less effective without manually initiating gc.collect()
      • requires the ability to do partial/incremental collection. It might not be possible without significant rework in the micropython GC code
        • but should be far less work than supporting reference counting
        • and could potentially remain a compile time configuration
  • minimize how often collection is required by avoiding new allocations
    • combined with manual control of collection timing, this can keep things as predictable as possible (although faster measurement certainly helps)
    • but at least for my use case, it's not practical to eliminate this entirely since using *args, **kwds in parameters or invocations usually creates temporaries that will have to be collected

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions