GC FAQ - Draft: Common Questions
GC FAQ - Draft: Common Questions
Common questions
What is garbage collection? Garbage collection is a part of a language's runtime system, or an add-on library, perhaps assisted by the compiler, the hardware, the OS, or any combination of the three, that automatically determines what memory a program is no longer using, and recycles it for other use. It is also known as ``automatic storage (or memory) reclamation''. Why is it good? Manual memory management is (programmer-)time consuming, and error prone. Most programs still contain leaks. This is all doubly true with programs using exception-handling and/or threads. A second benefit of garbage collection, less obvious to people who haven't used it, is that relying on garbage collection to manage memory simplifies the interfaces between components (subroutines, libraries, modules, classes) that no longer need expose memory management details ("who is responsible for recycling this memory").
Is garbage collection slow? Not necessarily. Modern garbage collectors appear to run as quickly as manual storage allocators (malloc/free or new/delete). Garbage collection probably will not run as quickly as customized memory allocator designed for use in a specific program. On the other hand, the extra code required to make manual memory management work properly (for example, explicit reference counting) is often more expensive than a garbage collector would be. Can I use garbage collection with C or C++? Probably. Modern (well-tested, efficient, non-pausing) garbage collectors are available that work with all but the most pathological C and C++ programs, including legacy code. See GC, C, and C++ for more details. Does garbage collection cause my program's execution to pause? Not necessarily. A variety of algorithms allow garbage collection to proceed concurrently, incrementally, and (for some definitions of the term) in "real time". There are incremental garbage collectors that work with C and C++, for instance. Where can I get a C or C++ garbage collector? Boehm-Weiser collector http://reality.sgi.com/employees/boehm_mti/gc.html or ftp://parcftp.xerox.com/pub/gc/gc.html
Great Circle from Geodesic Systems <sales@geodesic.com> or 800-360-8388 or http://www.geodesic.com/ Kevin Warne <warne@direct.ca> or 800-707-7171
Folk myths
GC is necessarily slower than manual memory management. GC will necessarily make my program pause. Manual memory management won't cause pauses. GC is incompatible with C and C++.
Folk truths
Most allocated objects are dynamically referenced by a very small number of pointers. The most important small number is ONE. Most allocated objects have short lifetimes. Allocation patterns (size distributions, lifetime distributions) are bursty, not uniform. VM behavior matters. Cache behavior matters. "Optimal" strategies can fail miserably.
Tradeoffs
precise vs. conservative moving/compacting vs. non-moving explicit vs. implicit reclamation phase stopping vs. incremental vs. concurrent generational vs. non-generational
memory, but where it is not, the garbage collector does the job. This doesn't necessarily run any faster than free-does-nothing, but it may help keep the heap smaller.
This doesn't sound very portable. What if I need to port my code and there's no garbage collector on the target platform?
Some of this code is necessarily system-dependent, but the features of most operating systems have been enumerated, so garbage collection for C is available almost everywhere. That is, portability isn't a problem if the code has already been ported, and it has. Speaking personally (this is David Chase) it's also not hard to port these garbage collectors to new platforms; I've ported the Boehm-Weiser collector twice myself, when the code had not yet been ported to terribly many platforms, and when I had much less experience with the low-level interfaces to various operating systems.
somewhere in an address space, so certain things won't work. For instance, the XOR'd pointers trick for compactly encoding a bidirectional list cannot be used -- the pointers don't look like pointers. If a process writes pointers to a file, and reads them back again, the memory referenced by those pointers may have been recycled. Most programs don't do these things, so most programs work with a garbage collector. Ordinary (legal) pointer arithmetic is tolerated by garbage collectors for C. Insert more questions here -- send them to <gclist@iecc.com>
2. What happens when registered objects reference each other?; 3. What happens if a finalization function makes an object not be garbage any more? There are no pat answers to these questions.