什么是Python gc.get_count()返回的count0,count1和count2值 [英] what are count0, count1 and count2 values returned by the Python gc.get_count()

查看:570
本文介绍了什么是Python gc.get_count()返回的count0,count1和count2值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python的gc软件包的文档说明了有关gc.get_count()的信息:

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

这是一个示例程序:

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())

以下是输出:

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)

很明显,count0 = 75,count1 = 5,count2 = 1.

什么是count0,count1和count2?

解决方案

gc软件包中的"gc"是指垃圾收集".它是一组动手方法,您可以通过这些方法来接管系统中的垃圾收集任务.这样做可以释放资源并简化程序的执行效率.但是应该注意,系统垃圾收集管理已经相当不错了.是的,系统会为您创建和引用的每个变量创建一堆对象和其他开销,是的,开销可能会持续很长时间(即使您调用了del命令),但总的来说没有任何害处. /p>

gc.get_count()返回一个元组,指示系统正在保留的对象引用的内部整理计数.这些是系统可以(自动)收集垃圾时知道的计数.

元组的三个值中的每个值代表三代中的对象(引用). Python总共列出了三代产品.每当对象幸免于垃圾收集事件时,它就会移至下一代. (75,5,1)表示最新一代有75个对象,中代有5个对象,最老一代有1个对象.

计数是内部系统值,而不是您分配或直接控制的值.您可以使用gc.collect()重置一些值,以释放大量资源;并且您可以使用gc.get_threshold()和gc.set_threshold()设置系统用于触发垃圾回收事件的触发值.但是对于除了最顽固的编码人员和开发人员(我当然不是其中之一)之外的所有人员而言,垃圾回收最好留在python系统上.

这能回答问题吗?

信用: 从stactify.com/python-garbage-collection/

收集了有关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?

解决方案

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

这篇关于什么是Python gc.get_count()返回的count0,count1和count2值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆