Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / module-gc.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-weakref.html" />
<link rel="prev" href="module-sys.html" />
<link rel="parent" href="python.html" />
<link rel="next" href="module-weakref.html" />
<meta name='aesop' content='information' />
<title>3.2 gc -- Garbage Collector interface</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.1 sys "
href="module-sys.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
href="python.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.3 weakref "
href="module-weakref.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-sys.html">3.1 sys </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-weakref.html">3.3 weakref </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION005200000000000000000">
3.2 <tt class="module">gc</tt> --
Garbage Collector interface</A>
</H1>
<P>
<A NAME="module-gc"></A>
<P>
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
<code>gc.disable()</code>. To debug a leaking program call
<code>gc.set_debug(gc.DEBUG_LEAK)</code>. Notice that this includes
<code>gc.DEBUG_SAVEALL</code>, causing garbage-collected objects to be
saved in gc.garbage for inspection.
<P>
The <tt class="module">gc</tt> module provides the following functions:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-399' xml:id='l2h-399' class="function">enable</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Enable automatic garbage collection.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-400' xml:id='l2h-400' class="function">disable</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Disable automatic garbage collection.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-401' xml:id='l2h-401' class="function">isenabled</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Returns true if automatic collection is enabled.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-402' xml:id='l2h-402' class="function">collect</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Run a full collection. All generations are examined and the
number of unreachable objects found is returned.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-403' xml:id='l2h-403' class="function">set_debug</tt></b>(</nobr></td>
<td><var>flags</var>)</td></tr></table></dt>
<dd>
Set the garbage collection debugging flags.
Debugging information will be written to <code>sys.stderr</code>. See below
for a list of debugging flags which can be combined using bit
operations to control debugging.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-404' xml:id='l2h-404' class="function">get_debug</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the debugging flags currently set.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-405' xml:id='l2h-405' class="function">get_objects</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Returns a list of all objects tracked by the collector, excluding the
list returned.
<span class="versionnote">New in version 2.2.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-406' xml:id='l2h-406' class="function">set_threshold</tt></b>(</nobr></td>
<td><var>threshold0</var><big>[</big><var>,
threshold1</var><big>[</big><var>, threshold2</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Set the garbage collection thresholds (the collection frequency).
Setting <var>threshold0</var> to zero disables collection.
<P>
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 <code>0</code>). If an object survives a
collection it is moved into the next older generation. Since
generation <code>2</code> 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
<var>threshold0</var>, collection starts. Initially only generation
<code>0</code> is examined. If generation <code>0</code> has been examined more
than <var>threshold1</var> times since generation <code>1</code> has been
examined, then generation <code>1</code> is examined as well. Similarly,
<var>threshold2</var> controls the number of collections of generation
<code>1</code> before collecting generation <code>2</code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-407' xml:id='l2h-407' class="function">get_threshold</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the current collection thresholds as a tuple of
<code>(<var>threshold0</var>, <var>threshold1</var>, <var>threshold2</var>)</code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-408' xml:id='l2h-408' class="function">get_referrers</tt></b>(</nobr></td>
<td><var>*objs</var>)</td></tr></table></dt>
<dd>
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.
<P>
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 <tt class="function">collect()</tt> before calling
<tt class="function">get_referrers()</tt>.
<P>
Care must be taken when using objects returned by
<tt class="function">get_referrers()</tt> because some of them could still be under
construction and hence in a temporarily invalid state. Avoid using
<tt class="function">get_referrers()</tt> for any purpose other than debugging.
<P>
<span class="versionnote">New in version 2.2.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-409' xml:id='l2h-409' class="function">get_referents</tt></b>(</nobr></td>
<td><var>*objs</var>)</td></tr></table></dt>
<dd>
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 <tt class="member">tp_traverse</tt> methods (if any), and may not be all
objects actually directly reachable. <tt class="member">tp_traverse</tt> 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.
<P>
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
The following variable is provided for read-only access (you can
mutate its value but should not rebind it):
<P>
<dl><dt><b><tt id='l2h-410' xml:id='l2h-410'>garbage</tt></b></dt>
<dd>
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 <tt class="method">__del__()</tt> methods.<A NAME="tex2html13"
HREF="#foot5795"><SUP>3.1</SUP></A>Objects that have
<tt class="method">__del__()</tt> 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 <tt class="method">__del__()</tt>
methods. If you know a safe order, you can force the issue by examining
the <var>garbage</var> 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 <var>garbage</var> list, so they should be
removed from <var>garbage</var> too. For example, after breaking cycles, do
<code>del gc.garbage[:]</code> to empty the list. It's generally better
to avoid the issue by not creating cycles containing objects with
<tt class="method">__del__()</tt> methods, and <var>garbage</var> can be examined in that
case to verify that no such cycles are being created.
<P>
If <tt class="constant">DEBUG_SAVEALL</tt> is set, then all unreachable objects will
be added to this list rather than freed.
</dd></dl>
<P>
The following constants are provided for use with
<tt class="function">set_debug()</tt>:
<P>
<dl><dt><b><tt id='l2h-411' xml:id='l2h-411'>DEBUG_STATS</tt></b></dt>
<dd>
Print statistics during collection. This information can
be useful when tuning the collection frequency.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-412' xml:id='l2h-412'>DEBUG_COLLECTABLE</tt></b></dt>
<dd>
Print information on collectable objects found.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-413' xml:id='l2h-413'>DEBUG_UNCOLLECTABLE</tt></b></dt>
<dd>
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 <code>garbage</code> list.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-414' xml:id='l2h-414'>DEBUG_INSTANCES</tt></b></dt>
<dd>
When <tt class="constant">DEBUG_COLLECTABLE</tt> or <tt class="constant">DEBUG_UNCOLLECTABLE</tt> is
set, print information about instance objects found.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-415' xml:id='l2h-415'>DEBUG_OBJECTS</tt></b></dt>
<dd>
When <tt class="constant">DEBUG_COLLECTABLE</tt> or <tt class="constant">DEBUG_UNCOLLECTABLE</tt> is
set, print information about objects other than instance objects found.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-416' xml:id='l2h-416'>DEBUG_SAVEALL</tt></b></dt>
<dd>
When set, all unreachable objects found will be appended to
<var>garbage</var> rather than being freed. This can be useful for debugging
a leaking program.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-417' xml:id='l2h-417'>DEBUG_LEAK</tt></b></dt>
<dd>
The debugging flags necessary for the collector to print
information about a leaking program (equal to <code>DEBUG_COLLECTABLE |
DEBUG_UNCOLLECTABLE | DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL</code>).
</dd></dl>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot5795">... methods.</A><A
href="module-gc.html#tex2html13"><SUP>3.1</SUP></A></DT>
<DD>Prior to
Python 2.2, the list contained all instance objects in unreachable
cycles, not only those with <tt class="method">__del__()</tt> methods.
</DD>
</DL>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="3.1 sys "
href="module-sys.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
href="python.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.3 weakref "
href="module-weakref.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-sys.html">3.1 sys </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-weakref.html">3.3 weakref </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>