📄 a garbage collector for c and c++.htm
字号:
<!DOCTYPE HTML><html><head><title>A garbage collector for C and C++</title></head><body dragover="true">
<table bgcolor="#f0f0ff" cellpadding="10%">
<tbody><tr>
<td><a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/gcinterface.html">Interface Overview</a></td>
<td><a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/04tutorial.pdf">Tutorial Slides</a></td>
<td><a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/faq.html">FAQ</a></td>
<td><a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/simple_example.html">Example</a></td>
<td><a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/gc_source">Download</a></td>
<td><a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/license.txt">License</a></td>
</tr>
</tbody></table>
<h1>A garbage collector for C and C++</h1>
<ul>
<li><a href="#where">Where to get the collector</a>
</li><li><a href="#platforms">Platforms</a>
</li><li><a href="#multiprocessors">Scalable multiprocessor versions</a>
</li><li><a href="#details">Some collector details</a>
</li><li><a href="#further">Further reading</a>
</li><li><a href="#users">Current users</a>
</li><li><a href="#collector">Local Links for this collector</a>
</li><li><a href="#background">Local Background Links</a>
</li><li><a href="#contacts">Contacts and Mailing List</a>
</li></ul>
[ This is an updated version of the page formerly at
<tt>http://reality.sgi.com/boehm/gc.html</tt>
and before that at
<a href="ftp://parcftp.xerox.com/pub/gc/gc.html">
<tt>ftp://parcftp.xerox.com/pub/gc/gc.html</tt></a>.]
<p>
The <a href="http://www.hpl.hp.com/personal/Hans_Boehm">Boehm</a>-<a href="http://www.cs.cornell.edu/annual_report/00-01/bios.htm#demers">Demers</a>-<a href="http://www-sul.stanford.edu/weiser/">Weiser</a>
conservative garbage collector can
be used as a garbage collecting
replacement for C <tt>malloc</tt> or C++ <tt>new</tt>.
It allows you to allocate memory basically as you normally would,
without explicitly deallocating memory that is no longer useful.
The collector automatically recycles memory when it determines
that it can no longer be otherwise accessed.
A simple example of such a use is given
<a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/simple_example.html">here</a>.
</p><p>
The collector is also used by a number of programming language
implementations that either use C as intermediate code, want
to facilitate easier interoperation with C libraries, or
just prefer the simple collector interface.
For a more detailed description of the interface, see
<a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/gcinterface.html">here</a>.
</p><p>
Alternatively, the garbage collector may be used as
a <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/leak.html">leak detector</a>
for C or C++ programs, though that is not its primary goal.
</p><p>
The arguments for and against conservative garbage collection
in C and C++ are briefly
discussed in
<a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/issues.html">issues.html</a>. The beginnings of
a frequently-asked-questions list are <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/faq.html">here</a>.
</p><p>
Empirically, this collector works with most unmodified C programs,
simply by replacing
<tt>malloc</tt> with <tt>GC_malloc</tt> calls,
replacing <tt>realloc</tt> with <tt>GC_realloc</tt> calls, and removing
free calls. Exceptions are discussed
in <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/issues.html">issues.html</a>.
</p><h2><a name="where">Where to get the collector</a></h2>
<p>
Typically several versions will be available.
Usually you should first try to use
<a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/gc_source/gc.tar.gz"><tt>gc_source/gc.tar.gz</tt></a>,
which is normally an older, more stable version.
</p><p>
If that fails, try the latest explicitly numbered version
in <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/gc_source/">
<tt>gc_source/</tt></a>.
Later versions may contain additional features, platform support,
or bug fixes, but are likely to be less well tested.
Note that versions containing the letters <tt>alpha</tt> are even less
well tested than others, especially on non-HP platforms.
</p><p>
The latest experimental version of the source code is now maintained
on the SourceForge site (project "bdwgc"). It can be browsed
<a href="http://bdwgc.cvs.sourceforge.net/bdwgc/">here</a>.
</p><p>
To anonymously check out a CVS version use:
<br>
<tt>cvs -d:pserver:anonymous@bdwgc.cvs.sourceforge.net:/cvsroot/bdwgc login
<br>
cvs -z3 -d:pserver:anonymous@bdwgc.cvs.sourceforge.net:/cvsroot/bdwgc co -P bdwgc</tt>
</p><p>
Maintainers can check out a version for development with
<br>
<tt>cvs -z3 -d:ext:<name>@bdwgc.cvs.sourceforge.net:/cvsroot/bdwgc co -P bdwgc</tt>
<br>
with <tt>CVS_RSH</tt> set to ssh, or whatever is needed to satisfy
your firewall.
</p><p>
A slightly older version of the garbage collector is now also
included as part of the
<a href="http://gcc.gnu.org/">GNU compiler</a>
distribution. The source
code for that version is available for browsing
<a href="http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/boehm-gc/">here</a>.
</p><p>
The garbage collector code is copyrighted by
<a href="http://www.hpl.hp.com/personal/Hans_Boehm">Hans-J. Boehm</a>,
Alan J. Demers,
<a href="http://www.xerox.com/">Xerox Corporation</a>,
<a href="http://www.sgi.com/">Silicon Graphics</a>,
and
<a href="http://www.hp.com/">Hewlett-Packard Company</a>.
It may be used and copied without payment of a fee under minimal restrictions.
See the README file in the distribution or the
<a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/license.txt">license</a> for more details.
<b>IT IS PROVIDED AS IS,
WITH ABSOLUTELY NO WARRANTY EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK</b>.
</p><h2><a name="platforms">Platforms</a></h2>
The collector is not completely portable, but the distribution
includes ports to most standard PC and UNIX/Linux platforms.
The collector should work on Linux, *BSD, recent Windows versions,
MacOS X, HP/UX, Solaris,
Tru64, Irix and a few other operating systems.
Some ports are more polished than others.
There are <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/porting.html">instructions</a> for porting the collector
to a new platform.
<p>
Irix pthreads, Linux threads, Win32 threads, Solaris threads
(old style and pthreads),
HP/UX 11 pthreads, Tru64 pthreads, and MacOS X threads are supported
in recent versions.
</p><h3>Separately distributed ports</h3>
For MacOS 9/Classic use, Patrick Beard's latest port is available from
<a href="http://homepage.mac.com/pcbeard/gc/">
<tt>http://homepage.mac.com/pcbeard/gc/</tt></a>.
(Unfortunately, that's now quite dated.
I'm not in a position to test under MacOS. Although I try to
incorporate changes, it is impossible for
me to update the project file.)
<p>
Precompiled versions of the collector for NetBSD are available
<a href="ftp://ftp.netbsd.org/pub/NetBSD/packages/pkgsrc/devel/boehm-gc/README.html">here</a>
or
<a href="http://www.netbsd.org/packages/devel/boehm-gc/README.html">here</a>.
</p><p>
<a href="http://www.debian.org/">Debian Linux</a> includes prepackaged
versions of the collector.
</p><h2><a name="multiprocessors">Scalable multiprocessor versions</a></h2>
Kenjiro Taura, Toshio Endo, and Akinori Yonezawa have made available
a <a href="http://www.yl.is.s.u-tokyo.ac.jp/gc/">parallel collector</a>
based on this one. Their collector takes advantage of multiple processors
during a collection. Starting with collector version 6.0alpha1
we also do this, though with more modest processor scalability goals.
Our approach is discussed briefly in
<a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/scale.html"><tt>scale.html</tt></a>.
<h2><a name="details">Some Collector Details</a></h2>
The collector uses a <a dragover="true" href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/complexity.html">mark-sweep</a> algorithm.
It provides incremental and generational
collection under operating systems which provide the right kind of
virtual memory support. (Currently this includes SunOS[45], IRIX,
OSF/1, Linux, and Windows, with varying restrictions.)
It allows <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/finalization.html"><i>finalization</i></a> code
to be invoked when an object is collected.
It can take advantage of type information to locate pointers if such
information is provided, but it is usually used without such information.
ee the README and
<tt>gc.h</tt> files in the distribution for more details.
<p>
For an overview of the implementation, see <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/gcdescr.html">here</a>.
</p><p>
The garbage collector distribution includes a C string
(<a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/gc_source/cordh.txt"><i>cord</i></a>) package that provides
for fast concatenation and substring operations on long strings.
A simple curses- and win32-based editor that represents the entire file
as a cord is included as a
sample application.
</p><p>
Performance of the nonincremental collector is typically competitive
with malloc/free implementations. Both space and time overhead are
likely to be only slightly higher
for programs written for malloc/free
(see Detlefs, Dosser and Zorn's
<a href="ftp://ftp.cs.colorado.edu/pub/techreports/zorn/CU-CS-665-93.ps.Z">Memory Allocation Costs in Large C and C++ Programs</a>.)
For programs allocating primarily very small objects, the collector
may be faster; for programs allocating primarily large objects it will
be slower. If the collector is used in a multithreaded environment
and configured for thread-local allocation, it may in some cases
significantly outperform malloc/free allocation in time.
</p><p>
We also expect that in many cases any additional overhead
will be more than compensated for by decreased copying etc.
if programs are written
and tuned for garbage collection.
</p><h1><a name="further">Further Reading:</a></h1>
<b>The beginnings of a frequently asked questions list for this
collector are <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/faq.html">here</a></b>.
<p>
<b>The following provide information on garbage collection in general</b>:
</p><p>
Paul Wilson's <a href="ftp://ftp.cs.utexas.edu/pub/garbage">garbage collection ftp archive</a> and <a href="ftp://ftp.cs.utexas.edu/pub/garbage/gcsurvey.ps">GC survey</a>.
</p><p>
The Ravenbrook <a href="http://www.memorymanagement.org/">
Memory Management Reference</a>.
</p><p>
David Chase's
<a href="http://www.iecc.com/gclist/GC-faq.html">GC FAQ</a>.
</p><p>
Richard Jones'
<a href="http://www.ukc.ac.uk/computer_science/Html/Jones/gc.html">
GC page</a> and
<a href="http://www.cs.kent.ac.uk/people/staff/rej/gcbook/gcbook.html">
his book</a>.
</p><p>
<b>The following papers describe the collector algorithms we use
and the underlying design decisions at
a higher level.</b>
</p><p>
(Some of the lower level details can be found
<a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/gcdescr.html">here</a>.)
</p><p>
The first one is not available
electronically due to copyright considerations. Most of the others are
subject to ACM copyright.
</p><p>
Boehm, H., "Dynamic Memory Allocation and Garbage Collection", <i>Computers in Physics
9</i>, 3, May/June 1995, pp. 297-303. This is directed at an otherwise sophisticated
audience unfamiliar with memory allocation issues. The algorithmic details differ
from those in the implementation. There is a related letter to the editor and a minor
correction in the next issue.
</p><p>
Boehm, H., and <a href="http://www.ubiq.com/hypertext/weiser/weiser.html">M. Weiser</a>,
<a href="http://www.hpl.hp.com/personal/Hans_Boehm/spe_gc_paper">"Garbage Collection in an Uncooperative Environment"</a>,
<i>Software Practice & Experience</i>, September 1988, pp. 807-820.
</p><p>
Boehm, H., A. Demers, and S. Shenker, <a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/papers/pldi91.ps.Z">"Mostly Parallel Garbage Collection"</a>, Proceedings
of the ACM SIGPLAN '91 Conference on Programming Language Design and Implementation,
<i>SIGPLAN Notices 26</i>, 6 (June 1991), pp. 157-164.
</p><p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -