Boehm-Demers-Weiser garbage collector,也就是著名的Boehm GC,是計算機應用在C/C++語言上的一個保守的垃圾回收器,可應用於許多經由C/C++開發的專案,同時也適用於其它執行環境的各類程式語言,包括了GNU版Java編譯器執行環境,以及Mono的Microsoft .NET移植平台。同時支援許多的作業平台,如各種Unix作業系統,微軟的作業系統(Microsoft Windows),以及麥金塔上的作業系統(Mac OS X),還有更進一步的功能,例如:漸進式收集(incremental collection),平行收集(parallel collection)以及終結語意的變化(variety of finalizer semantics)。
範例
垃圾收集器作用於未變性的(unmodified)C程式,只要簡單的將malloc呼叫用GC_malloc取代,將realloc取代為GC_realloc呼叫,如此一來便不需要使用到free的函式。下列的程式碼展示出如何用Boehm取代傳統的malloc以及free。[https://web.archive.org/web/20090210083741/http://www.hpl.hp.com/personal/Hans_Boehm/gc/simple_example.html].
#include
#include
#include
int main(void)
{
int i;
const int size = 10000000;
GC_INIT();
for (i = 0; i
外部链接
*
*
- [https://github.com/ivmai/bdwgc/ Git repo for BoehmGC development]
- [http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2310.pdf Transparent Programmer-Directed Garbage Collection for C++, Hans-J. Boehm and Michael Spertus]
- [https://www.assembla.com/spaces/hito1/documents/biqNQMOhur3AwCab7jnrAJ/download/UsingtheC_CGarbageCollectionLibrarylibgc.pdf Using the C/C++ Garbage Collection Library]
- [http://www.drdobbs.com/the-boehm-collector-for-c-and-c/184401632 Dr. Dobbs The Boehm Collector for C and C++, Gene Michael Stover, March 01, 2003]
评论 (0)