为什么在重新分配小块内存时分配大块内存失败

Why does allocating large chunks of memory fail when reallocing small chunks doesn't

此代码导致 x 指向一块大小为 100GB 的内存。

#include <stdlib.h>
#include <stdio.h>

int main() {
    auto x = malloc(1);
    for (int i = 1; i< 1024; ++i) x = realloc(x, i*1024ULL*1024*100);
    while (true); // Give us time to check top
}

虽然此代码分配失败。

#include <stdlib.h>
#include <stdio.h>

int main() {
    auto x = malloc(1024ULL*1024*100*1024);
    printf("%llu\n", x);
    while (true); // Give us time to check top
}

好吧,你在成功的那个中分配了更少的内存:

for (int i = 1; i< 1024; ++i) x = realloc(x, i*1024ULL*1024*100);

最后一个realloc是:

x = realloc(x, 1023 * (1024ULL*1024*100));

相比于:

auto x = malloc(1024 * (1024ULL*100*1024));

也许这就是您的内存边界所在 - 摔断骆驼背的最后 100M?

我的猜测是,您系统的内存大小小于您尝试分配的 100 GiB。虽然 Linux 确实会过度使用内存,但它仍然会摆脱超出其能力范围的请求。这就是第二个示例失败的原因。

另一方面,第一个示例的许多小增量远低于该阈值。因此,它们中的每一个都成功了,因为内核知道您还不需要任何先前的内存,因此它没有迹象表明它将无法支持这 100 个额外的 MiB。

我认为进程内存请求失败的阈值与可用 RAM 相关,并且可以调整(虽然我不记得具体是多少)。