boehm-gc:终结器,缩小以适应堆

时间:2013-12-22 06:40:29

标签: c++ garbage-collection boehm-gc

我有2个关于boehm-gc的问题。

  1. 当GC收集垃圾对象,GC空闲内存而不调用析构函数,尽管该对象具有析构函数。我发现GC称为“finailzer”,但我不知道如何注册...我该怎么办?

  2. 当GC收集垃圾时,GC似乎不会调用free()(或其他无内存函数)。 GC似乎没有释放垃圾,而是把它放到GC的内存池中并在下次分配时使用池。 GC在空闲时释放内存池?如果没有,我可以对GC说“请释放内存池”吗?

  3. PS。我找不到boehm-gc参考..你能告诉我参考的位置吗?

1 个答案:

答案 0 :(得分:3)

如果您需要的参考文件比gc.h头文件中提供的参考文件多,那么您可能应该先阅读垃圾收集器,然后再继续阅读。

你有问题,gc.h标题符合你的要求:

typedef void (*GC_finalization_proc)
  GC_PROTO((GC_PTR obj, GC_PTR client_data));

GC_API void GC_register_finalizer
    GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
      GC_finalization_proc *ofn, GC_PTR *ocd));
GC_API void GC_debug_register_finalizer
    GC_PROTO((GC_PTR obj, GC_finalization_proc fn, GC_PTR cd,
      GC_finalization_proc *ofn, GC_PTR *ocd));
/* When obj is no longer accessible, invoke     */
/* (*fn)(obj, cd).  If a and b are inaccessible, and    */
/* a points to b (after disappearing links have been    */
/* made to disappear), then only a will be      */
/* finalized.  (If this does not create any new     */
/* pointers to b, then b will be finalized after the    */
/* next collection.)  Any finalizable object that   */
/* is reachable from itself by following one or more    */
/* pointers will not be finalized (or collected).   */
/* Thus cycles involving finalizable objects should */
/* be avoided, or broken by disappearing links.     */
/* All but the last finalizer registered for an object  */
/* is ignored.                      */
/* Finalization may be removed by passing 0 as fn.  */
/* Finalizers are implicitly unregistered just before   */
/* they are invoked.                    */
/* The old finalizer and client data are stored in  */
/* *ofn and *ocd.                   */ 
/* Fn is never invoked on an accessible object,     */
/* provided hidden pointers are converted to real   */
/* pointers only if the allocation lock is held, and    */
/* such conversions are not performed by finalization   */
/* routines.                        */
/* If GC_register_finalizer is aborted as a result of   */
/* a signal, the object may be left with no     */
/* finalization, even if neither the old nor new    */
/* finalizer were NULL.                 */
/* Obj should be the nonNULL starting address of an     */
/* object allocated by GC_malloc or friends.        */
/* Note that any garbage collectable object referenced  */
/* by cd will be considered accessible until the    */
/* finalizer is invoked.                */

所以你定义了一个回调:

typedef <any type at all you want passed to the callback
         as data for its own use> MY_ENVIRONMENT;

void my_callback(GC_PTR void_obj, GC_PTR void_environment) {
  MY_ENVIRONMENT *env = (MY_ENVIRONMENT)void_environment;
  MY_OBJECT *obj = (MY_OBJECT*)void_obj;

  // Do finalization here.
}

创建其环境(如果有的话;否则只传递NULL):

MY_ENVIRONMENT *my_env = new MY_ENVIRONMENT;
// Initialize if necessary.

然后在新分配的对象上注册它:

MY_
MY_ENVIRONMENT old_env;
GC_finalization_proc old_proc;
GC_register_finalizer(new_obj, my_callback, my_env, &old_env, &old_proc);

当收集此特定对象时,将使用您的环境记录调用my_callback

关于你的问题2,你忽略了这一点。 Boehm GC 取代 malloc / new并免费管理自己的内存竞技场。它通常决定何时进行收集。这通常是大多数竞技场已用完的时候。垃圾收集标识了空闲的块,因此有资格重新分配。正如API notes清楚地说,你可以强制收集和无力对象,但这些通常是没有必要的。