gpt4 book ai didi

garbage-collection - 关于OCaml GC的minor/young heap

转载 作者:行者123 更新时间:2023-12-01 02:16:36 33 4
gpt4 key购买 nike

我正在阅读 the GC, Chapter 21 in Real World OCaml ,并有一些关于次要堆的问题。

所以它说:

The minor heap is a contiguous chunk of virtual memory that is usually a few megabytes in size so that it can be scanned quickly.



enter image description here

The runtime stores the boundaries of the minor heap in two pointers that delimit the start and end of the heap region (caml_young_start and caml_young_end, but we will drop the caml_young prefix for brevity). The base is the memory address returned by the system malloc, and start is aligned against the next nearest word boundary from base to make it easier to store OCaml values.

In a fresh minor heap, the limit equals the start, and the current ptr will equal the end. ptr decreases as blocks are allocated until it reaches limit, at which point a minor garbage collection is triggered.

You may wonder why limit is required at all, since it always seems to equal start. It's because the easiest way for the runtime to schedule a minor heap collection is by setting limit to equal end. The next allocation will never have enough space after this is done and will always trigger a garbage collection. There are various internal reasons for such early collections, such as handling pending UNIX signals, and they don't ordinarily matter for application code.



Q1:minor heap和base的关系

基本上,运行时将尝试使用系统 malloc 分配一块内存,例如,大小为 8 MB,作为次要堆?

那么malloc返回的地址是base?

Q2:base和start的关系
start is aligned against the next nearest word boundary from base是什么意思?

这里对齐的术语是否意味着 start 将寻找下一个 mods 4 等于 0 的内存地址?

我认为 malloc 无论如何都会强制对齐,即始终返回 mod 4 = 0 的地址

Q3:当我们有限制时,我们使用什么开始?

它解释说可以使用限制来安排 GC。

但是,那start有什么用呢?

最佳答案

您可以在 minor_gc.c 中找到您要询问的代码. (我不是 OCaml GC 高手——我不得不四处寻找代码。)

这是一个精简版:

void caml_set_minor_heap_size (asize_t size)
{
char *new_heap;
void *new_heap_base;

if (caml_young_ptr != caml_young_end) caml_minor_collection ();
new_heap = caml_aligned_malloc(size, 0, &new_heap_base);
if (new_heap == NULL) caml_raise_out_of_memory();
if (caml_page_table_add(In_young, new_heap, new_heap + size) != 0)
caml_raise_out_of_memory();

if (caml_young_start != NULL){
caml_page_table_remove(In_young, caml_young_start, caml_young_end);
free (caml_young_base);
}
caml_young_base = new_heap_base;
caml_young_start = new_heap;
caml_young_end = new_heap + size;
caml_young_limit = caml_young_start;
caml_young_ptr = caml_young_end;
caml_minor_heap_size = size;

reset_table (&caml_ref_table);
reset_table (&caml_weak_ref_table);
}

base 是 malloc 返回的块的实际起始地址.稍后当您想要释放块时需要它(如果您想更改次堆的大小,则需要这样做)。据我所知,底座没有其他用途。

开头是 base 的对齐版本。我正在查看的代码(OCaml 4.01.0)与页面边界(4 KB)对齐。 Malloc 仅针对原始数据类型(8 个字节左右)对齐。

如摘录的最后一段所述,在人为修改限制后,您需要开始重置限制。

关于garbage-collection - 关于OCaml GC的minor/young heap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24231862/

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