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:
程序員的自我修養