[英]boehm-gc : finalizer, and shrink to fit heap
我有2個關於boehm-gc的問題。
當GC收集垃圾對象,GC空閑內存而不調用析構函數,盡管該對象具有析構函數。 我發現GC稱為“finailzer”,但我不知道如何注冊它......我該怎么辦?
當GC收集垃圾時,GC似乎不會調用free()(或其他無內存函數)。 GC似乎沒有釋放垃圾,而是把它放到GC的內存池中並在下次分配時使用池。 GC在空閑時釋放內存池? 如果沒有,我可以對GC說“請釋放內存池”嗎?
PS。 我找不到boehm-gc參考..你能告訴我參考的位置嗎?
如果你需要比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和free並管理自己的內存競技場。 它通常決定何時進行收集。 這通常是大多數競技場已用完的時候。 垃圾收集標識了空閑的塊,因此有資格重新分配。 正如API筆記中明確指出的那樣,您可以強制收集和無力對象,但這些通常不是必需的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.