linux的虚拟地址空间分配给进程本身是3GB,windows默认是2GB,那我究竟到底能malloc多大的内存呢?
#include <stdio.h>
#include <stdlib.h>
unsigned int max = 0;
int main()
{
unsigned int blocksize[] = {1024 * 1024, 1024, 1};
int i, count;
for (i = 0; i < 3; i++)
{
for (count = 1; ; count++)
{
void* block = malloc(max + blocksize[i] * count);
if (block) //malloc ok
{
max += blocksize[i] * count;
free(block);
}
else
{
break;
}
}
}
printf("max malloc size = %uB\n", max);
return 0;
}
gcc -> run
2939899735 B 约= 2.7 GB
清晰明了了吧。
P.S. 文献说在windows下运行这个程序结果大约为1.5GB :)
reference:
程序员的自我修养