C++ 的垃圾收集(garbage collector

C++ 中實做 gc 有一個有名的 implementation,叫做 Boehm garbage collector。包括 gcj 或 mono 等知名的軟體專案都有採用。
http://patricklog.blogspot.com/2011/02/c-garbage-collector.html
/*
  * gctest.cpp
  *
  *  Created on: 2011/2/27
  *      Author: Patrick Wang
  */
 #include <gc/gc_cpp.h>
 #include <iostream>
 
 #define THIS "this: "<<this
 
 using namespace std;
 
 class ClassA: public virtual gc_cleanup {
 public:
   ClassA() { cout<<"Construction "<<THIS<<endl; }
   ~ClassA() { cout<<"Destruction "<<THIS<<endl; }
   int a[100000];
 
 };
 
 int main() {
   GC_INIT();
   cout<<">>> GC Heap size: "<<GC_get_heap_size()<<endl;
   for (int i = 0; i < 100; i++){
     ClassA *a = new ClassA();
     cout<<">>> GC Heap size: "<<GC_get_heap_size()<<endl;
   }
 }

Powered by Jekyll and Theme by solid

本站总访问量