即使物理RAM小于3 GB,这个有趣的代码也总是在Linux系统中分配3 GB的内存。为什么这种内存分配的怪异行为
怎么样? (我在我的系统中的2.7 GB的RAM和该代码分配3.054 MB内存!)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
void *ptr;
int n = 0;
while (1) {
// Allocate in 1 MB chunks
ptr = malloc(0x100000);
// Stop when we can't allocate any more
if (ptr == NULL)
break;
n++;
}
// How much did we get?
printf("malloced %d MB\n", n);
pause();
}
您是否启用了交换文件/分区? – user2422531
@ user2422531:我有,但我禁用了交换,但仍然得到了相同的结果。 – Inquisitive
在OS下查找网络上的虚拟内存。 – 0decimal0