I have occasionally come across code that manually calls gc.collect()
, but it's unclear why. For what reasons, if any, would it be advantageous to manually run a garbage collection, rather than let Python handle it automatically?
In short, efficiency.
If you are using a pointer to reference something, it is using memory. Even when you stop pointing at it, it still takes up memory for a short amount of time.
Python's garbage collection used to use reference counting. Reference counting is a smart way to tell when something should no longer need memory (by checking how often it is referenced by other objects).
Unfortunately, reference counting is still a calculation so it reduces efficiency to run it all the time (especially when it is not needed), so instead Python now uses scheduled garbage collection.
The issue is that in some specific functionality Python now doesn't garbage collect immediately when it notices something is no longer used (as in the case with reference counting) because it garbage collects periodically instead.
You generally need to run garbage collection manually in a lot of server applications or large scale calculations where minor infractions in garbage collection matter to free memory.
Here is a link from Digi.com that sums it up perfectly if my explanation isn't clear. I hope this helps Brendan!