Search code examples
pythongarbage-collectionpython-internals

what are count0, count1 and count2 values returned by the Python gc.get_count()


The documentation for python's gc package says this about gc.get_count():

gc.get_count()
    Return the current collection counts as a tuple of (count0, count1, count2).

Here is a sample program:

import gc


if __name__=="__main__":
    print("making some data")
    for k in range(10):
        root = [range(i,1000) for i in range(1,1000)]
    print("len(gc.get_objects):",len(gc.get_objects()))
    print("gc.get_stats:",gc.get_stats())
    print("gc.get_count:",gc.get_count())

Here is the output:

making some data
len(gc.get_objects): 7130
gc.get_stats: [{'collections': 16, 'collected': 99, 'uncollectable': 0}, {'collections': 1, 'collected': 0, 'uncollectable': 0}, {'collections': 0, 'collected': 0, 'uncollectable': 0}]
gc.get_count: (75, 5, 1)

Clearly, count0 = 75, count1=5, and count2=1.

What are count0, count1 and count2?


Solution

  • Unfortunately both the answer given by @user10637953, and the referenced article are incorrect.

    count0 are the (tracked object allocations - deallocations) that happened since last garbage collection.

    Sometime after it reaches the gen0 threshold (defaults to 700), a gen0 garbage collection will occur, and count0 will reset.

    count1 is the number of gen0 collections since the last gen1 collection. Upon reaching the threshold (defaults to 10), gen1 collection will occur, and count1 will reset.

    count2 is the number of gen1 collections since the last gen2 collection. Upon reaching the threshold (defaults to 10 as well), gen2 collection will occur, and count2 will reset.

    You can easily prove this yourself by running gc.collect(gen), and see the thresholds using gc.get_threshold().

    For more info, see the official dev guide.