gc — Garbage Collector interface¶

This module provides an interface to the optional garbage collector. It provides the ability to disable the collector, tune the collection frequency, and set debugging options. It also provides access to unreachable objects that the collector found but cannot free. Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles. Automatic collection can be disabled by calling gc.disable() . To debug a leaking program call gc.set_debug(gc.DEBUG_LEAK) . Notice that this includes gc.DEBUG_SAVEALL , causing garbage-collected objects to be saved in gc.garbage for inspection.

The gc module provides the following functions:

gc. enable ( ) ¶ Enable automatic garbage collection.

gc. disable ( ) ¶ Disable automatic garbage collection.

gc. isenabled ( ) ¶ Returns true if automatic collection is enabled.

gc. collect ( generation=2 ) ¶ With no arguments, run a full collection. The optional argument generation may be an integer specifying which generation to collect (from 0 to 2). A ValueError is raised if the generation number is invalid. The number of unreachable objects found is returned. The free lists maintained for a number of built-in types are cleared whenever a full collection or collection of the highest generation (2) is run. Not all items in some free lists may be freed due to the particular implementation, in particular float .

gc. set_debug ( flags ) ¶ Set the garbage collection debugging flags. Debugging information will be written to sys.stderr . See below for a list of debugging flags which can be combined using bit operations to control debugging.

gc. get_debug ( ) ¶ Return the debugging flags currently set.

gc. get_objects ( ) ¶ Returns a list of all objects tracked by the collector, excluding the list returned.

gc. get_stats ( ) ¶ Return a list of three per-generation dictionaries containing collection statistics since interpreter start. The number of keys may change in the future, but currently each dictionary will contain the following items: collections is the number of times this generation was collected;

is the number of times this generation was collected; collected is the total number of objects collected inside this generation;

is the total number of objects collected inside this generation; uncollectable is the total number of objects which were found to be uncollectable (and were therefore moved to the garbage list) inside this generation. New in version 3.4.

gc. set_threshold ( threshold0 [ , threshold1 [ , threshold2 ] ] ) ¶ Set the garbage collection thresholds (the collection frequency). Setting threshold0 to zero disables collection. 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 ( ) ¶ Return the current collection counts as a tuple of (count0, count1, count2) .

gc. get_threshold ( ) ¶ Return the current collection thresholds as a tuple of (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.

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