【发布时间】:2014-02-20 22:55:25
【问题描述】:
我有以下在 Ubuntu 上以 C 语言运行的代码。它计算操作系统可以通过 malloc() 分配多少 GB。
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
int count = 0;
char* a;
while (1){
a = (char*)malloc(1024*1024*1024);
if (a==NULL) break;
count++;
}
printf("%d\n", count);
return 0;
}
令人惊讶的是,在我的机器上运行时,它打印了超过 100,000 个。我觉得这太不合理了。我的 RAM 是 8GB,我的硬盘大约是 500 GB,这 100,000 是从哪里来的?
【问题讨论】: