这是Mono Continuations的continuation_store(...)。从下面的代码看,似乎store()跟随这两个分支:
cont->saved_stack && num_bytes <= cont->stack_alloc_size
然而,奇怪的是,如果我重复使用continuation_store(),内存使用量会增加,直到稍后的步骤才会进行大量且滞后的GC操作。任何人都可以解释为什么会这样吗?
由于
static int
continuation_store (MonoContinuation *cont, int state, MonoException **e)
{
MonoLMF *lmf = mono_get_lmf ();
gsize num_bytes;
if (!cont->domain)
*e = mono_get_exception_argument ("cont", "Continuation not initialized");
if (cont->domain != mono_domain_get () || cont->thread_id != GetCurrentThreadId ())
*e = mono_get_exception_argument ("cont", "Continuation from another thread or domain");
cont->lmf = lmf;
cont->return_ip = __builtin_return_address (0);
cont->return_sp = __builtin_frame_address (0);
num_bytes = (char*)cont->top_sp - (char*)cont->return_sp;
/*g_print ("store: %d bytes, sp: %p, ip: %p, lmf: %p\n", num_bytes, cont->return_sp, cont->return_ip, lmf);*/
if (cont->saved_stack && num_bytes <= cont->stack_alloc_size)
{
/* clear to avoid GC retention */
if (num_bytes < cont->stack_used_size)
memset ((char*)cont->saved_stack + num_bytes, 0, cont->stack_used_size - num_bytes);
}
else
{
tasklets_lock ();
internal_init ();
if (cont->saved_stack) {
mono_g_hash_table_remove (keepalive_stacks, cont->saved_stack);
mono_gc_free_fixed (cont->saved_stack);
}
cont->stack_used_size = num_bytes;
cont->stack_alloc_size = num_bytes * 1.1;
cont->saved_stack = mono_gc_alloc_fixed (cont->stack_alloc_size, NULL);
mono_g_hash_table_insert (keepalive_stacks, cont->saved_stack, cont->saved_stack);
tasklets_unlock ();
}
memcpy (cont->saved_stack, cont->return_sp, num_bytes);
return state;
}
答案 0 :(得分:1)
请注意,对mono_gc_free_fixed的调用在默认的boehm收集器中不执行任何操作:https://github.com/mono/mono/blob/master/mono/metadata/boehm-gc.c#L528
else分支只是从keepalive_stacks哈希表中删除内存。此哈希表保留对已分配内存的引用,因此不会对其进行垃圾回收。一旦从该哈希表中删除了指向已分配内存的指针,它就会在下一次垃圾回收期间被回收。一旦达到一定的内存分配,此集合将在稍后触发。