/Doc/library/gc.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 217 lines · 137 code · 80 blank · 0 comment · 0 complexity · d108f807d8db5854f4a0306df145b05d MD5 · raw file

  1. :mod:`gc` --- Garbage Collector interface
  2. =========================================
  3. .. module:: gc
  4. :synopsis: Interface to the cycle-detecting garbage collector.
  5. .. moduleauthor:: Neil Schemenauer <nas@arctrix.com>
  6. .. sectionauthor:: Neil Schemenauer <nas@arctrix.com>
  7. This module provides an interface to the optional garbage collector. It
  8. provides the ability to disable the collector, tune the collection frequency,
  9. and set debugging options. It also provides access to unreachable objects that
  10. the collector found but cannot free. Since the collector supplements the
  11. reference counting already used in Python, you can disable the collector if you
  12. are sure your program does not create reference cycles. Automatic collection
  13. can be disabled by calling ``gc.disable()``. To debug a leaking program call
  14. ``gc.set_debug(gc.DEBUG_LEAK)``. Notice that this includes
  15. ``gc.DEBUG_SAVEALL``, causing garbage-collected objects to be saved in
  16. gc.garbage for inspection.
  17. The :mod:`gc` module provides the following functions:
  18. .. function:: enable()
  19. Enable automatic garbage collection.
  20. .. function:: disable()
  21. Disable automatic garbage collection.
  22. .. function:: isenabled()
  23. Returns true if automatic collection is enabled.
  24. .. function:: collect([generation])
  25. With no arguments, run a full collection. The optional argument *generation*
  26. may be an integer specifying which generation to collect (from 0 to 2). A
  27. :exc:`ValueError` is raised if the generation number is invalid. The number of
  28. unreachable objects found is returned.
  29. .. versionchanged:: 2.5
  30. The optional *generation* argument was added.
  31. .. versionchanged:: 2.6
  32. The free lists maintained for a number of builtin types are cleared
  33. whenever a full collection or collection of the highest generation (2)
  34. is run. Not all items in some free lists may be freed due to the
  35. particular implementation, in particular :class:`int` and :class:`float`.
  36. .. function:: set_debug(flags)
  37. Set the garbage collection debugging flags. Debugging information will be
  38. written to ``sys.stderr``. See below for a list of debugging flags which can be
  39. combined using bit operations to control debugging.
  40. .. function:: get_debug()
  41. Return the debugging flags currently set.
  42. .. function:: get_objects()
  43. Returns a list of all objects tracked by the collector, excluding the list
  44. returned.
  45. .. versionadded:: 2.2
  46. .. function:: set_threshold(threshold0[, threshold1[, threshold2]])
  47. Set the garbage collection thresholds (the collection frequency). Setting
  48. *threshold0* to zero disables collection.
  49. The GC classifies objects into three generations depending on how many
  50. collection sweeps they have survived. New objects are placed in the youngest
  51. generation (generation ``0``). If an object survives a collection it is moved
  52. into the next older generation. Since generation ``2`` is the oldest
  53. generation, objects in that generation remain there after a collection. In
  54. order to decide when to run, the collector keeps track of the number object
  55. allocations and deallocations since the last collection. When the number of
  56. allocations minus the number of deallocations exceeds *threshold0*, collection
  57. starts. Initially only generation ``0`` is examined. If generation ``0`` has
  58. been examined more than *threshold1* times since generation ``1`` has been
  59. examined, then generation ``1`` is examined as well. Similarly, *threshold2*
  60. controls the number of collections of generation ``1`` before collecting
  61. generation ``2``.
  62. .. function:: get_count()
  63. Return the current collection counts as a tuple of ``(count0, count1,
  64. count2)``.
  65. .. versionadded:: 2.5
  66. .. function:: get_threshold()
  67. Return the current collection thresholds as a tuple of ``(threshold0,
  68. threshold1, threshold2)``.
  69. .. function:: get_referrers(*objs)
  70. Return the list of objects that directly refer to any of objs. This function
  71. will only locate those containers which support garbage collection; extension
  72. types which do refer to other objects but do not support garbage collection will
  73. not be found.
  74. Note that objects which have already been dereferenced, but which live in cycles
  75. and have not yet been collected by the garbage collector can be listed among the
  76. resulting referrers. To get only currently live objects, call :func:`collect`
  77. before calling :func:`get_referrers`.
  78. Care must be taken when using objects returned by :func:`get_referrers` because
  79. some of them could still be under construction and hence in a temporarily
  80. invalid state. Avoid using :func:`get_referrers` for any purpose other than
  81. debugging.
  82. .. versionadded:: 2.2
  83. .. function:: get_referents(*objs)
  84. Return a list of objects directly referred to by any of the arguments. The
  85. referents returned are those objects visited by the arguments' C-level
  86. :attr:`tp_traverse` methods (if any), and may not be all objects actually
  87. directly reachable. :attr:`tp_traverse` methods are supported only by objects
  88. that support garbage collection, and are only required to visit objects that may
  89. be involved in a cycle. So, for example, if an integer is directly reachable
  90. from an argument, that integer object may or may not appear in the result list.
  91. .. versionadded:: 2.3
  92. The following variable is provided for read-only access (you can mutate its
  93. value but should not rebind it):
  94. .. data:: garbage
  95. A list of objects which the collector found to be unreachable but could not be
  96. freed (uncollectable objects). By default, this list contains only objects with
  97. :meth:`__del__` methods. [#]_ Objects that have :meth:`__del__` methods and are
  98. part of a reference cycle cause the entire reference cycle to be uncollectable,
  99. including objects not necessarily in the cycle but reachable only from it.
  100. Python doesn't collect such cycles automatically because, in general, it isn't
  101. possible for Python to guess a safe order in which to run the :meth:`__del__`
  102. methods. If you know a safe order, you can force the issue by examining the
  103. *garbage* list, and explicitly breaking cycles due to your objects within the
  104. list. Note that these objects are kept alive even so by virtue of being in the
  105. *garbage* list, so they should be removed from *garbage* too. For example,
  106. after breaking cycles, do ``del gc.garbage[:]`` to empty the list. It's
  107. generally better to avoid the issue by not creating cycles containing objects
  108. with :meth:`__del__` methods, and *garbage* can be examined in that case to
  109. verify that no such cycles are being created.
  110. If :const:`DEBUG_SAVEALL` is set, then all unreachable objects will be added to
  111. this list rather than freed.
  112. The following constants are provided for use with :func:`set_debug`:
  113. .. data:: DEBUG_STATS
  114. Print statistics during collection. This information can be useful when tuning
  115. the collection frequency.
  116. .. data:: DEBUG_COLLECTABLE
  117. Print information on collectable objects found.
  118. .. data:: DEBUG_UNCOLLECTABLE
  119. Print information of uncollectable objects found (objects which are not
  120. reachable but cannot be freed by the collector). These objects will be added to
  121. the ``garbage`` list.
  122. .. data:: DEBUG_INSTANCES
  123. When :const:`DEBUG_COLLECTABLE` or :const:`DEBUG_UNCOLLECTABLE` is set, print
  124. information about instance objects found.
  125. .. data:: DEBUG_OBJECTS
  126. When :const:`DEBUG_COLLECTABLE` or :const:`DEBUG_UNCOLLECTABLE` is set, print
  127. information about objects other than instance objects found.
  128. .. data:: DEBUG_SAVEALL
  129. When set, all unreachable objects found will be appended to *garbage* rather
  130. than being freed. This can be useful for debugging a leaking program.
  131. .. data:: DEBUG_LEAK
  132. The debugging flags necessary for the collector to print information about a
  133. leaking program (equal to ``DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE |
  134. DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL``).
  135. .. rubric:: Footnotes
  136. .. [#] Prior to Python 2.2, the list contained all instance objects in unreachable
  137. cycles, not only those with :meth:`__del__` methods.