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

前端 未结 1 553
攒了一身酷
攒了一身酷 2021-01-19 07:50

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, cou         


        
相关标签:
1条回答
  • 2021-01-19 08:25

    'gc' in the gc package refers to 'garbage collection'. It is a set of hands-on methods by which you can take over garbage collection duties from the system. Doing so can free up resources and streamline the efficiency of your program execution. But it should be noted that the system garbage collection management is already pretty dang good. Yes, the system creates a bunch of objects and other overhead for every variable you create and reference, and yes that overhead can linger for a long time (even if you call a del command), but on the whole it does no harm.

    gc.get_count() returns a tuple indicating the internal housekeeping counts of object-references that the system is keeping. These are the counts by which the system will know when it is OK to (automatically) collect the garbage.

    Each of the tuple's three values represent objects (references) in three generations. Python keeps a list of three generations in all. Each time an object survives a garbage collection event, it is move up to the next generation. (75, 5, 1) mean there are 75 objects in the newest generation, 5 objects in the middle generation and 1 object in the oldest generation.

    The counts are internal system values and not values you assign or have (much) direct control over. You can reset some values using gc.collect(), freeing up large blocks of resources; and you can set the trigger values the system uses to trip garbage collection events, using gc.get_threshold() and gc.set_threshold(). But for all but the hardest-core coders and developers (of which I certainly am not one) garbage collection is best left on the hands of the python system.

    Does this answer the question?

    Credit: The information on the gc.get_count() method was gleaned from stactify.com/python-garbage-collection/

    0 讨论(0)
提交回复
热议问题