我编写了这个程序来了解内存消耗模式。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
while(1 < 2) {
int *str = (int *) malloc(100000000);
if(str == NULL) {
printf("Out of memory.. %d", i);
return(1);
}
printf("Attempt Count = %d\n", i++);
}
return(0);
}
在我的系统中,我有8GB的内存。每个malloc调用将尝试存储100MB。因此,我期望我的程序在(10*8)次循环后会崩溃。但计数器远远超过了600,000。我无法理解这种情况。请帮忙解释一下。
即使考虑所有交换空间等,它也不应该增加超过16GB,这似乎在这里不太可能。