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;
   }
 }

相关文章:

  • 2021-10-11
  • 2021-11-19
  • 2021-12-12
  • 2022-01-10
  • 2021-08-18
  • 2022-01-23
  • 2021-07-18
猜你喜欢
  • 2022-12-23
  • 2021-11-17
  • 2021-11-25
  • 2021-12-08
  • 2021-04-10
  • 2022-02-23
  • 2021-10-06
相关资源
相似解决方案