#include <stdio.h>
#include <stdlib.h>
unsigned maxinum = 0;
int main()
{
unsigned blocksize[] = {1024 * 1024,1024,1};
int i,count;
for(i = 0; i < 3; i++)
{
for(count = 1;;count++)
{
void *block = malloc(maxinum + blocksize[i] * count);
if(block)
{
maxinum = maxinum + blocksize[i] * count;
free(block);
}
else
{
break;
}
}
}
printf("maxinum malloc size = %u bytes\n",maxinum);
return 0;
#include <stdlib.h>
unsigned maxinum = 0;
int main()
{
unsigned blocksize[] = {1024 * 1024,1024,1};
int i,count;
for(i = 0; i < 3; i++)
{
for(count = 1;;count++)
{
void *block = malloc(maxinum + blocksize[i] * count);
if(block)
{
maxinum = maxinum + blocksize[i] * count;
free(block);
}
else
{
break;
}
}
}
printf("maxinum malloc size = %u bytes\n",maxinum);
return 0;
}
结果:
说明linux大约2.9G的可用内存,还有1G系统内存,共4G