28.12. gc — 垃圾收集器接口

此模块提供到可选垃圾收集器的接口。它提供禁用收集器、微调收集频率及设置调试选项的能力。它还提供对收集器能发现却无法释放的无法企及对象的访问。由于收集器会增补 Python 已使用的引用计数,因此若确信程序不会创建引用循环,则可以禁用收集器。自动收集可以被禁用通过调用 gc.disable() 。要调试泄漏程序调用 gc.set_debug(gc.DEBUG_LEAK) 。预告,这包括 gc.DEBUG_SAVEALL ,导致垃圾收集对象被保存在 gc.garbage 中以供审查。

The gc 模块提供下列函数:

gc. enable ( )

启用自动垃圾收集。

gc. disable ( )

禁用自动垃圾收集。

gc. isenabled ( )

Returns true if automatic collection is enabled.

gc. collect ( [ generation ] )

没有自变量,运行完整收集。可选自变量 generation 可以是指定要收集哪一代 (从 0 到 2) 的整数。 ValueError 被引发若世代数无效。返回找到的无法企及对象数。

Changed in version 2.5: 可选 generation 自变量被添加。

2.6 版改变: 由许多内置类型维护的释放列表会被清零,每当运行完整收集或最高 2 世代的收集时。某些释放列表中的所有项可能不会被释放,由于特定实现原因,尤其是 int and float .

gc. set_debug ( flags )

设置垃圾收集调试标志。调试信息会被写入到 sys.stderr . See below for a list of debugging flags which can be combined using bit operations to control debugging.

gc. get_debug ( )

返回目前设置的调试标志。

gc. get_objects ( )

Returns a list of all objects tracked by the collector, excluding the list returned.

2.2 版新增。

gc. set_threshold ( threshold0 [ , threshold1 [ , threshold2 ] ] )

设置垃圾收集阈值 (收集频率)。设置 threshold0 为 0 禁用收集。

The GC classifies objects into three generations depending on how many collection sweeps they have survived. New objects are placed in the youngest generation (generation 0 ). If an object survives a collection it is moved into the next older generation. Since generation 2 is the oldest generation, objects in that generation remain there after a collection. 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 .

gc. get_count ( )

返回当前收集计数按元组 (count0, count1, count2) .

2.5 版新增。

gc. get_threshold ( )

返回当前收集阈值按元组 (threshold0, threshold1, threshold2) .

gc. get_referrers ( *objs )

Return the list of objects that directly refer to any of objs. This function will only locate those containers which support garbage collection; extension types which do refer to other objects but do not support garbage collection will not be found.

Note that objects which have already been dereferenced, but which live in cycles and have not yet been collected by the garbage collector can be listed among the resulting referrers. To get only currently live objects, call collect() before calling get_referrers() .

Care must be taken when using objects returned by get_referrers() because some of them could still be under construction and hence in a temporarily invalid state. Avoid using get_referrers() for any purpose other than debugging.

2.2 版新增。

gc. get_referents ( *objs )

Return a list of objects directly referred to by any of the arguments. The referents returned are those objects visited by the arguments’ C-level tp_traverse methods (if any), and may not be all objects actually directly reachable. tp_traverse methods are supported only by objects that support garbage collection, and are only required to visit objects that may be involved in a cycle. So, for example, if an integer is directly reachable from an argument, that integer object may or may not appear in the result list.

2.3 版新增。

gc. is_tracked ( obj )

返回 True if the object is currently tracked by the garbage collector, False otherwise. As a general rule, instances of atomic types aren’t tracked and instances of non-atomic types (containers, user-defined objects…) are. However, some type-specific optimizations can be present in order to suppress the garbage collector footprint of simple instances (e.g. dicts containing only atomic keys and values):

>>> gc.is_tracked(0)
False
>>> gc.is_tracked("a")
False
>>> gc.is_tracked([])
True
>>> gc.is_tracked({})
False
>>> gc.is_tracked({"a": 1})
False
>>> gc.is_tracked({"a": []})
True
						

2.7 版新增。

The following variable is provided for read-only access (you can mutate its value but should not rebind it):

gc. garbage

A list of objects which the collector found to be unreachable but could not be freed (uncollectable objects). By default, this list contains only objects with __del__() 方法。 1 Objects that have __del__() methods and are part of a reference cycle cause the entire reference cycle to be uncollectable, including objects not necessarily in the cycle but reachable only from it. Python doesn’t collect such cycles automatically because, in general, it isn’t possible for Python to guess a safe order in which to run the __del__() methods. If you know a safe order, you can force the issue by examining the garbage list, and explicitly breaking cycles due to your objects within the list. Note that these objects are kept alive even so by virtue of being in the garbage list, so they should be removed from garbage too. For example, after breaking cycles, do del gc.garbage[:] to empty the list. It’s generally better to avoid the issue by not creating cycles containing objects with __del__() methods, and garbage can be examined in that case to verify that no such cycles are being created.

DEBUG_SAVEALL is set, then all unreachable objects will be added to this list rather than freed.

The following constants are provided for use with set_debug() :

gc. DEBUG_STATS

Print statistics during collection. This information can be useful when tuning the collection frequency.

gc. DEBUG_COLLECTABLE

Print information on collectable objects found.

gc. DEBUG_UNCOLLECTABLE

Print information of uncollectable objects found (objects which are not reachable but cannot be freed by the collector). These objects will be added to the garbage 列表。

gc. DEBUG_INSTANCES

DEBUG_COLLECTABLE or DEBUG_UNCOLLECTABLE is set, print information about instance objects found.

gc. DEBUG_OBJECTS

DEBUG_COLLECTABLE or DEBUG_UNCOLLECTABLE is set, print information about objects other than instance objects found.

gc. DEBUG_SAVEALL

When set, all unreachable objects found will be appended to garbage rather than being freed. This can be useful for debugging a leaking program.

gc. DEBUG_LEAK

The debugging flags necessary for the collector to print information about a leaking program (equal to DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL ).

脚注

1

Prior to Python 2.2, the list contained all instance objects in unreachable cycles, not only those with __del__() 方法。

上一话题

28.11. __future__ — 未来的语句定义

下一话题

28.13. inspect — 审查存活对象

本页