gpt4 book ai didi

c++ - boehm-gc : finalizer, 并缩小以适应堆

转载 作者:行者123 更新时间:2023-11-30 02:50:04 25 4
gpt4 key购买 nike

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

  1. GC 回收垃圾对象时,即使对象有析构函数,GC 也会释放内存而不调用析构函数。我发现 GC 调用“finailzer”,但我不知道如何注册它...我该怎么办?

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

附言。我找不到 boehm-gc 引用..你能告诉我引用在哪里吗?

最佳答案

如果您需要比 gc.h 头文件中提供更多的引用,那么您可能应该在继续之前先阅读垃圾收集器。

关于你的问题,gc.h header 有你需要的:

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 和 free 并管理自己的内存区域。它通常自行决定何时进行收集。这通常是在大部分竞技场已用完时。垃圾收集识别空闲的 block ,因此有资格重新分配。作为API notes明确地说,您可以强制收集和强制收集对象,但这些通常不是必需的。

关于c++ - boehm-gc : finalizer, 并缩小以适应堆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20726948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com