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

依然范特西╮ 提交于 2020-06-15 04:13:29

问题


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?


回答1:


'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/



来源:https://stackoverflow.com/questions/57462037/what-are-count0-count1-and-count2-values-returned-by-the-python-gc-get-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!