嗨,有人知道linux进程中堆分配的上限是什么吗?考虑下面的例子,
int main() {
char *p;
unsigned long int cnt=0;
while(1) {
p = (char*)malloc(128*1024*1024); //128MB
cnt++;
cout <<cnt<<endl;
}
return 0;
}这个程序只有在大约200000次迭代后才会被杀死,这意味着它分配了128MB*200000=~25TB,我的系统本身有512 6GB的SSD +6GB的内存,这个程序如何能够分配25 to的内存?
发布于 2022-04-20 01:46:52
感谢Nate指向Why doesn't this memory eater really eat memory?,只有当我们将一些数据写入其中时,它才会消耗内存。当我像这样修改上面的程序时,它实际上在我的PC内存被完全消耗后就退出了(在4GB内存系统中,这是~3GB,我想1GB内存是为内核预留的)。
int main() {
char *p;
unsigned long int cnt=0;
size_t i=0, t = 128*1024*1024;
while(1) {
p = (char*)malloc(t);
#if 1
for (int i=0;i<t;i++)
*p++ = '0'+(i%65);
#endif
cnt++;
cout <<cnt<<endl;
}
return 0;
}https://stackoverflow.com/questions/71928358
复制相似问题