Movatterモバイル変換
[0]ホーム
This module provides an interface to the optional garbage collector. Itprovides the ability to disable the collector, tune the collectionfrequency, and set debugging options. It also provides access tounreachable objects that the collector found but cannot free. Since thecollector supplements the reference counting already used in Python, youcan disable the collector if you are sure your program does not createreference cycles. Automatic collection can be disabled by callinggc.disable(). To debug a leaking program callgc.set_debug(gc.DEBUG_LEAK). Notice that this includesgc.DEBUG_SAVEALL, causing garbage-collected objects to besaved in gc.garbage for inspection.
Thegc module provides the following functions:
- Enable automatic garbage collection.
- Disable automatic garbage collection.
- Returns true if automatic collection is enabled.
- With no arguments, run a full collection. The optional argumentgeneration may be an integer specifying which generation to collect(from 0 to 2). AValueError is raised if the generation number is invalid.The number of unreachable objects found is returned.
Changed in version 2.5:The optionalgeneration argument was added.
- Set the garbage collection debugging flags.Debugging information will be written to
sys.stderr. See belowfor a list of debugging flags which can be combined using bitoperations to control debugging.
- Return the debugging flags currently set.
- Returns a list of all objects tracked by the collector, excluding thelist returned.New in version 2.2.
| set_threshold( | threshold0[, threshold1[, threshold2]]) |
- Set the garbage collection thresholds (the collection frequency).Settingthreshold0 to zero disables collection.
The GC classifies objects into three generations depending on how manycollection sweeps they have survived. New objects are placed in theyoungest generation (generation0). If an object survives acollection it is moved into the next older generation. Sincegeneration2 is the oldest generation, objects in thatgeneration remain there after a collection. In order to decide whento run, the collector keeps track of the number object allocations anddeallocations since the last collection. When the number ofallocations minus the number of deallocations exceedsthreshold0, collection starts. Initially only generation0 is examined. If generation0 has been examined morethanthreshold1 times since generation1 has beenexamined, then generation1 is examined as well. Similarly,threshold2 controls the number of collections of generation1 before collecting generation2.
- Return the current collection counts as a tuple of
(count0,count1,count2).New in version 2.5.
- Return the current collection thresholds as a tuple of
(threshold0,threshold1,threshold2).
- Return the list of objects that directly refer to any of objs. Thisfunction will only locate those containers which support garbagecollection; extension types which do refer to other objects but do notsupport garbage collection will not be found.
Note that objects which have already been dereferenced, but which livein cycles and have not yet been collected by the garbage collector canbe listed among the resulting referrers. To get only currently liveobjects, callcollect() before callingget_referrers().
Care must be taken when using objects returned byget_referrers() because some of them could still be underconstruction and hence in a temporarily invalid state. Avoid usingget_referrers() for any purpose other than debugging.
New in version 2.2.
- Return a list of objects directly referred to by any of the arguments.The referents returned are those objects visited by the arguments'C-leveltp_traverse methods (if any), and may not be allobjects actually directly reachable.tp_traverse methodsare supported only by objects that support garbage collection, and areonly required to visit objects that may be involved in a cycle. So,for example, if an integer is directly reachable from an argument, thatinteger object may or may not appear in the result list.
New in version 2.3.
The following variable is provided for read-only access (you canmutate its value but should not rebind it):
- garbage
- A list of objects which the collector found to be unreachablebut could not be freed (uncollectable objects). By default, this listcontains only objects with__del__() methods.26.1Objects that have__del__() methods and are part of a reference cycle causethe entire reference cycle to be uncollectable, including objectsnot necessarily in the cycle but reachable only from it. Python doesn'tcollect such cycles automatically because, in general, it isn't possiblefor 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 examiningthegarbage list, and explicitly breaking cycles due to yourobjects within the list. Note that these objects are kept alive evenso by virtue of being in thegarbage list, so they should beremoved fromgarbage too. For example, after breaking cycles, do
del gc.garbage[:] to empty the list. It's generally betterto avoid the issue by not creating cycles containing objects with__del__() methods, andgarbage can be examined in thatcase to verify that no such cycles are being created.IfDEBUG_SAVEALL is set, then all unreachable objects willbe added to this list rather than freed.
The following constants are provided for use withset_debug():
- DEBUG_STATS
- Print statistics during collection. This information canbe useful when tuning the collection frequency.
- DEBUG_COLLECTABLE
- Print information on collectable objects found.
- DEBUG_UNCOLLECTABLE
- Print information of uncollectable objects found (objects which arenot reachable but cannot be freed by the collector). These objectswill be added to the
garbage list.
- DEBUG_INSTANCES
- WhenDEBUG_COLLECTABLE orDEBUG_UNCOLLECTABLE isset, print information about instance objects found.
- DEBUG_OBJECTS
- WhenDEBUG_COLLECTABLE orDEBUG_UNCOLLECTABLE isset, print information about objects other than instance objects found.
- DEBUG_SAVEALL
- When set, all unreachable objects found will be appended togarbage rather than being freed. This can be useful for debugginga leaking program.
- DEBUG_LEAK
- The debugging flags necessary for the collector to printinformation about a leaking program (equal to
DEBUG_COLLECTABLE |DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL).
Footnotes
- ... methods.26.1
- Prior to Python 2.2, the list contained all instance objects in unreachable cycles, not only those with__del__() methods.
Release 2.5.2, documentation updated on 21st February, 2008. SeeAbout this document... for information on suggesting changes.
[8]ページ先頭