📄 faq.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HEAD><TITLE>FAQ: A garbage collector for C and C++</title></head><BODY><H1>FAQ: A garbage collector for C and C++</h1>This is the beginning of a "frequently asked questions" file for whathas become known as the "Boehm-Demers-Weiser" garbage collector.Some of these are likely to apply to any garbage collector whatsoever.<P><B>I wrote a test program which allocates objects and registersfinalizers for them. Only a few (or no) objects are finalized.What's wrong?</b><P>Probably nothing. Finalizers are only executed if all ofthe following happen before the process exits:<UL><LI> A garbage collection runs. This normally happens only aftera significant amount of allocation.<LI> The objects in question appear inaccessible at the time ofthe collection. It is common for a handful of objects to appearaccessible even though they shouldn't be, e.g. because temporarypointers to them haven't yet been overwritten. Alsonote that by default only the first item in a chain of finalizableobjects will be finalized in a collection.<LI> Another GC_ call notices that there are finalizers waitingto be run and does so.</ul>Small test programs typically don't run long enough for this tohappen.<P><B>Does this mean that the collector might leak memory?</b><P>In the short term yes. But it is unlikely, though not impossible,that this will result in a leak that grows over time. Under normalcircumstances, short term, or one time leaks are a minor issue.Memory leaks in explicitly managed programs are feared because theyalmost always continue to grow over time.<P>For (a lot) more details see:<P>``Bounding Space Usage of Conservative Garbage Collectors'',<I>Proceedings of the 2002 ACM SIGPLAN-SIGACT Symposium on Principles ofProgramming Languages</i>, Jan. 2002, pp. 93-100.<A HREF="http://portal.acm.org/citation.cfm?doid=503272.503282">Official version.</a><A HREF="http://www.hpl.hp.com/techreports/2001/HPL-2001-251.html">Technical report version.</a><P><B>How can I get more of the finalizers to run to convince myself thatthe GC is working?</b><P>Invoke GC_gcollect a couple of times just before process exit.<P><B>I want to ensure that all my objects are finalized and reclaimed beforeprocess exit. How can I do that?</b><P>You can't, and you don't really want that.This would require finalizing <I>reachable</i> objects.Finalizers run later would have to be able to handle this, and would haveto be able to run with randomly broken libraries, because the objectsthey rely on where previously finalized. In most environments, youwould also be replacing the operating systems mechanism for veryefficiently reclaiming process memory at process exit with a significantlyslower mechanism.<P>You do sometimes want to ensure that certain particular resources areexplicitly reclaimed before process exit, whether or not they becomeunreachable. Programming techniques for ensuring this are discussed in<P>``Destructors, Finalizers, and Synchronization'',<I>Proceeedings of the 2003 ACM SIGPLAN-SIGACT Symposium on Principles ofProgramming Languages</i>, Jan. 2003, pp. 262-272.<A HREF="http://portal.acm.org/citation.cfm?doid=604131.604153">Official version.</a><A HREF="http://www.hpl.hp.com/techreports/2002/HPL-2002-335.html">Technical report version.</a><A HREF="../popl03/web/index.html">HTML slides.</a><A HREF="../popl03/slides.pdf">PDF slides.</a><P><B>I wrote a memory allocation loop, and it runs <I>much</i> slowerwith the garbage collector than when I use <TT>malloc</tt>/<TT>free</tt>memory management. Why?</b><P>Odds are your loop allocates very large objects and never initializesthem. Real programs generally don't behave that way. Garbage collectorsgenerally perform appreciably worse for large object allocations,and they generally initialize objects, even if you don't.<P><B>What can I do to maximize allocation performance?</b><P>Here are some hints:<UL><LI> Use <TT>GC_MALLOC_ATOMIC</tt> where possible.<LI> For a multithreaded application, take advantage of<TT>gc_local_alloc.h</tt> to avoid locking on each allocation.<LI> For a single-threaded application, use a GC library withoutthread support. If this is inconvenient, use <TT>gc_local_alloc.h</tt>.<LI> If you use large statically allocated arrays or mapped files,consider <TT>GC_exclude_static_roots</tt>.</ul><P><B>If my heap uses 2 GB on a 32-bit machine, won't every otherinteger or other random data be misinterpreted as a pointer by thecollector? Thus won't way too much memory be retained?</b><P>Maybe. Probably, if the collector is used purely conservatively,with no pointer layout information (such as use of<TT>GC_MALLOC_ATOMIC</tt>).<P>With a gigabyte heap, you are clearly much better off on a 64-bitmachine. Empirical evidence seems to suggest that some such applicationswork on a 32-bit machine, and others don't perform acceptably.<P>Simple probability calculations for pointer misidentificationsare generally incorrect.The probability of misinterpreting an integer is typically reducedsignificantly bya number of collector features and fortunate accidents. Most integersare small, and small integers can generally not be heap addresses.The collector black-listing mechanism avoids allocatingareas that are prone to be targets of misinterpreted references.The collector can be told to ignore some or all pointers to objectinteriors.<B>I have a different question that isn't answered here, nor in<A HREF="http://www.hpl.hp.com/personal/Hans_Boehm/gc">the other GCdocumentation</a>. Where else can I go?</b><P>You may want to check the<A HREF="http://www.hpl.hp.com/hosted/linux/mail-archives/gc/">the GC mailing list archives</a>.If you can't find the answer there, please post your question to<A HREF="mailto:gc@linux.hpl.hp.com">the GC mailing list</a>.If you are not subscribed to the mailing lists, your posting maytake a while to appear, since such postings are moderated.<P><B>Why can't I get to the mailing list administrative web siteto subscribe?</b><P>It's behind the HP firewall. (This is unintentional, but noteasily repaired.) Send mail to <TT>gc-request</tt> or<TT>gc-announce-request</tt> at <TT>linux.hpl.hp.com</tt> instead.<!-- Begin METRICS JavaScript --><!-- <script type="text/javascript" language="JavaScript" src="http://welcome.hp-ww.com/country/us/eng/js/metrics.js"></script> --> <script type="text/javascript" language="JavaScript" src="http://www.hpl.hp.com/dev/hplabsmetrics.js"></script><!-- End METRICS JavaScript --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -