cpython/Doc/library/gc.rst

336 lines
12 KiB
ReStructuredText
Raw Normal View History

:mod:`!gc` --- Garbage Collector interface
==========================================
2007-08-15 11:28:22 -03:00
.. module:: gc
:synopsis: Interface to the cycle-detecting garbage collector.
2007-08-15 11:28:22 -03:00
.. moduleauthor:: Neil Schemenauer <nas@arctrix.com>
.. sectionauthor:: Neil Schemenauer <nas@arctrix.com>
--------------
2007-08-15 11:28:22 -03:00
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 :mod:`gc` module provides the following functions:
.. function:: enable()
Enable automatic garbage collection.
.. function:: disable()
Disable automatic garbage collection.
.. function:: isenabled()
Return ``True`` if automatic collection is enabled.
2007-08-15 11:28:22 -03:00
.. function:: collect(generation=2)
2007-08-15 11:28:22 -03:00
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
:exc:`ValueError` is raised if the generation number is invalid. The sum of
collected objects and uncollectable objects is returned.
2007-08-15 11:28:22 -03:00
The free lists maintained for a number of built-in types are cleared
Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64722 | georg.brandl | 2008-07-05 12:13:36 +0200 (Sat, 05 Jul 2008) | 4 lines #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade. This is a new feature, but Barry authorized adding it in the beta period. ........ r64729 | mark.dickinson | 2008-07-05 13:33:52 +0200 (Sat, 05 Jul 2008) | 5 lines Issue 3188: accept float('infinity') as well as float('inf'). This makes the float constructor behave in the same way as specified by various other language standards, including C99, IEEE 754r, and the IBM Decimal standard. ........ r64753 | gregory.p.smith | 2008-07-06 05:35:58 +0200 (Sun, 06 Jul 2008) | 4 lines - Issue #2862: Make int and float freelist management consistent with other freelists. Changes their CompactFreeList apis into ClearFreeList apis and calls them via gc.collect(). ........ r64845 | raymond.hettinger | 2008-07-10 16:03:19 +0200 (Thu, 10 Jul 2008) | 1 line Issue 3301: Bisect functions behaved badly when lo was negative. ........ r64846 | raymond.hettinger | 2008-07-10 16:34:57 +0200 (Thu, 10 Jul 2008) | 1 line Issue 3285: Fractions from_float() and from_decimal() accept Integral arguments. ........ r64849 | andrew.kuchling | 2008-07-10 16:43:31 +0200 (Thu, 10 Jul 2008) | 1 line Wording changes ........ r64871 | raymond.hettinger | 2008-07-11 14:00:21 +0200 (Fri, 11 Jul 2008) | 1 line Add cautionary note on the use of PySequence_Fast_ITEMS. ........ r64880 | amaury.forgeotdarc | 2008-07-11 23:28:25 +0200 (Fri, 11 Jul 2008) | 5 lines #3317 in zipfile module, restore the previous names of global variables: some applications relied on them. Also remove duplicated lines. ........ r64881 | amaury.forgeotdarc | 2008-07-11 23:45:06 +0200 (Fri, 11 Jul 2008) | 3 lines #3342: In tracebacks, printed source lines were not indented since r62555. #3343: Py_DisplaySourceLine should be a private function. Rename it to _Py_DisplaySourceLine. ........ r64882 | josiah.carlson | 2008-07-12 00:17:14 +0200 (Sat, 12 Jul 2008) | 2 lines Fix for the AttributeError in test_asynchat. ........ r64885 | josiah.carlson | 2008-07-12 01:26:59 +0200 (Sat, 12 Jul 2008) | 2 lines Fixed test for asyncore. ........ r64888 | matthias.klose | 2008-07-12 09:51:48 +0200 (Sat, 12 Jul 2008) | 2 lines - Fix bashisms in Tools/faqwiz/move-faqwiz.sh ........ r64897 | benjamin.peterson | 2008-07-12 22:16:19 +0200 (Sat, 12 Jul 2008) | 1 line fix various doc typos #3320 ........ r64900 | alexandre.vassalotti | 2008-07-13 00:06:53 +0200 (Sun, 13 Jul 2008) | 2 lines Fixed typo. ........ r64901 | benjamin.peterson | 2008-07-13 01:41:19 +0200 (Sun, 13 Jul 2008) | 1 line #1778443 robotparser fixes from Aristotelis Mikropoulos ........ r64915 | nick.coghlan | 2008-07-13 16:52:36 +0200 (Sun, 13 Jul 2008) | 1 line Fix issue 3221 by emitting a RuntimeWarning instead of raising SystemError when the parent module can't be found during an absolute import (likely due to non-PEP 361 aware code which sets a module level __package__ attribute) ........ r64926 | martin.v.loewis | 2008-07-13 22:31:49 +0200 (Sun, 13 Jul 2008) | 2 lines Add turtle into the module index. ........ r64927 | alexandre.vassalotti | 2008-07-13 22:42:44 +0200 (Sun, 13 Jul 2008) | 3 lines Issue #3274: Use a less common identifier for the temporary variable in Py_CLEAR(). ........ r64928 | andrew.kuchling | 2008-07-13 23:43:25 +0200 (Sun, 13 Jul 2008) | 1 line Re-word ........ r64929 | andrew.kuchling | 2008-07-13 23:43:52 +0200 (Sun, 13 Jul 2008) | 1 line Add various items; move ctypes items into a subsection of their own ........ r64938 | andrew.kuchling | 2008-07-14 02:35:32 +0200 (Mon, 14 Jul 2008) | 1 line Typo fixes ........ r64939 | andrew.kuchling | 2008-07-14 02:40:55 +0200 (Mon, 14 Jul 2008) | 1 line Typo fix ........ r64940 | andrew.kuchling | 2008-07-14 03:18:16 +0200 (Mon, 14 Jul 2008) | 1 line Typo fix ........ r64941 | andrew.kuchling | 2008-07-14 03:18:31 +0200 (Mon, 14 Jul 2008) | 1 line Expand the multiprocessing section ........ r64944 | gregory.p.smith | 2008-07-14 08:06:48 +0200 (Mon, 14 Jul 2008) | 7 lines Fix posix.fork1() / os.fork1() to only call PyOS_AfterFork() in the child process rather than both parent and child. Does anyone actually use fork1()? It appears to be a Solaris thing but if Python is built with pthreads on Solaris, fork1() and fork() should be the same. ........ r64961 | jesse.noller | 2008-07-15 15:47:33 +0200 (Tue, 15 Jul 2008) | 1 line multiprocessing/connection.py patch to remove fqdn oddness for issue 3270 ........ r64966 | nick.coghlan | 2008-07-15 17:40:22 +0200 (Tue, 15 Jul 2008) | 1 line Add missing NEWS entry for r64962 ........ r64973 | jesse.noller | 2008-07-15 20:29:18 +0200 (Tue, 15 Jul 2008) | 1 line Revert 3270 patch: self._address is in pretty widespread use, need to revisit ........
2008-07-16 09:55:28 -03:00
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 :class:`float`.
The effect of calling ``gc.collect()`` while the interpreter is already
performing a collection is undefined.
2007-08-15 11:28:22 -03:00
.. function:: 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.
.. function:: get_debug()
Return the debugging flags currently set.
.. function:: get_objects(generation=None)
2007-08-15 11:28:22 -03:00
Returns a list of all objects tracked by the collector, excluding the list
returned. If *generation* is not ``None``, return only the objects tracked by
the collector that are in that generation.
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.8
New *generation* parameter.
2007-08-15 11:28:22 -03:00
.. audit-event:: gc.get_objects generation gc.get_objects
.. function:: 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;
* ``collected`` 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 :data:`garbage`
list) inside this generation.
.. versionadded:: 3.4
.. function:: set_threshold(threshold0, [threshold1, [threshold2]])
2007-08-15 11:28:22 -03:00
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.
With the third generation, things are a bit more complicated,
see `Collecting the oldest generation <https://devguide.python.org/garbage_collector/#collecting-the-oldest-generation>`_ for more information.
2007-08-15 11:28:22 -03:00
.. function:: get_count()
Return the current collection counts as a tuple of ``(count0, count1,
count2)``.
.. function:: get_threshold()
Return the current collection thresholds as a tuple of ``(threshold0,
threshold1, threshold2)``.
.. function:: 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 :func:`collect`
before calling :func:`get_referrers`.
.. warning::
Care must be taken when using objects returned by :func:`get_referrers` because
some of them could still be under construction and hence in a temporarily
invalid state. Avoid using :func:`get_referrers` for any purpose other than
debugging.
2007-08-15 11:28:22 -03:00
.. audit-event:: gc.get_referrers objs gc.get_referrers
2007-08-15 11:28:22 -03:00
.. function:: 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
:c:member:`~PyTypeObject.tp_traverse` methods (if any), and may not be all objects actually
directly reachable. :c:member:`~PyTypeObject.tp_traverse` methods are supported only by objects
2007-08-15 11:28:22 -03:00
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.
.. audit-event:: gc.get_referents objs gc.get_referents
2007-08-15 11:28:22 -03:00
.. function:: is_tracked(obj)
Returns ``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
2009-05-05 06:29:50 -03:00
.. versionadded:: 3.1
.. function:: is_finalized(obj)
Returns ``True`` if the given object has been finalized by the
garbage collector, ``False`` otherwise. ::
>>> x = None
>>> class Lazarus:
... def __del__(self):
... global x
... x = self
...
>>> lazarus = Lazarus()
>>> gc.is_finalized(lazarus)
False
>>> del lazarus
>>> gc.is_finalized(x)
True
.. versionadded:: 3.9
.. function:: freeze()
Freeze all the objects tracked by the garbage collector; move them to a
permanent generation and ignore them in all the future collections.
If a process will ``fork()`` without ``exec()``, avoiding unnecessary
copy-on-write in child processes will maximize memory sharing and reduce
overall memory usage. This requires both avoiding creation of freed "holes"
in memory pages in the parent process and ensuring that GC collections in
child processes won't touch the ``gc_refs`` counter of long-lived objects
originating in the parent process. To accomplish both, call ``gc.disable()``
early in the parent process, ``gc.freeze()`` right before ``fork()``, and
``gc.enable()`` early in child processes.
.. versionadded:: 3.7
.. function:: unfreeze()
Unfreeze the objects in the permanent generation, put them back into the
oldest generation.
.. versionadded:: 3.7
.. function:: get_freeze_count()
Return the number of objects in the permanent generation.
.. versionadded:: 3.7
The following variables are provided for read-only access (you can mutate the
values but should not rebind them):
2007-08-15 11:28:22 -03:00
.. data:: garbage
A list of objects which the collector found to be unreachable but could
not be freed (uncollectable objects). Starting with Python 3.4, this
list should be empty most of the time, except when using instances of
C extension types with a non-``NULL`` ``tp_del`` slot.
If :const:`DEBUG_SAVEALL` is set, then all unreachable objects will be
added to this list rather than freed.
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.2
If this list is non-empty at :term:`interpreter shutdown`, a
:exc:`ResourceWarning` is emitted, which is silent by default. If
:const:`DEBUG_UNCOLLECTABLE` is set, in addition all uncollectable objects
are printed.
.. versionchanged:: 3.4
Following :pep:`442`, objects with a :meth:`~object.__del__` method don't end
up in :data:`gc.garbage` anymore.
.. data:: callbacks
A list of callbacks that will be invoked by the garbage collector before and
after collection. The callbacks will be called with two arguments,
*phase* and *info*.
2012-09-30 14:36:07 -03:00
*phase* can be one of two values:
"start": The garbage collection is about to start.
"stop": The garbage collection has finished.
2012-09-30 14:36:07 -03:00
*info* is a dict providing more information for the callback. The following
keys are currently defined:
"generation": The oldest generation being collected.
"collected": When *phase* is "stop", the number of objects
successfully collected.
2012-09-30 14:36:07 -03:00
"uncollectable": When *phase* is "stop", the number of objects
that could not be collected and were put in :data:`garbage`.
Applications can add their own callbacks to this list. The primary
use cases are:
Gathering statistics about garbage collection, such as how often
various generations are collected, and how long the collection
takes.
Allowing applications to identify and clear their own uncollectable
types when they appear in :data:`garbage`.
.. versionadded:: 3.3
2007-08-15 11:28:22 -03:00
The following constants are provided for use with :func:`set_debug`:
.. data:: DEBUG_STATS
Print statistics during collection. This information can be useful when tuning
the collection frequency.
.. data:: DEBUG_COLLECTABLE
Print information on collectable objects found.
.. data:: 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`` list.
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.2
Also print the contents of the :data:`garbage` list at
:term:`interpreter shutdown`, if it isn't empty.
2007-08-15 11:28:22 -03:00
.. data:: 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.
.. data:: DEBUG_LEAK
The debugging flags necessary for the collector to print information about a
leaking program (equal to ``DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE |
DEBUG_SAVEALL``).