Search code examples
pythonpython-2.7garbage-collectionpython-internals

Why the default gc generation threshold is set to (700, 10, 10) in Python


Why the default GC generation threshold is set to (700, 10, 10) in Python; And the latter two numbers 10, 10 are so small?

Does such low threshold would result in much more collections?


Solution

  • The thresholds are multipliers relative to the previous generation. From the gc.set_threshold() documentation:

    In order to decide when to run, the collector keeps track of the number object allocations and deallocations since the last collection. When the number of allocations minus the number of deallocations exceeds threshold0, collection starts. Initially only generation 0 is examined. If generation 0 has been examined more than threshold1 times since generation 1 has been examined, then generation 1 is examined as well. Similarly, threshold2 controls the number of collections of generation 1 before collecting generation 2.

    Bold emphasis mine.

    So generation 1 is only run if generation 0 has been run 10 times. Since generation 0 only runs every 700 (delta of allocation - deallocation) cycles, that means generation 1 is run every 10 * 700 == 7.000 cycles. Generation 2 is run every 10 * 10 * 700 == 70.000 cycles.