Crane
Table_bottom

Search
Loading
Table_bottom

分类
Table_bottom

随机文章
Table_bottom

标签云
Table_bottom

最新评论
Table_bottom

链接
Table_bottom

功能
Table_bottom

测试堆的最大申请数量

Crane posted @ 2010年1月14日 06:50 in Linux with tags c 编程 linux , 4925 阅读

看到说linux下的虚拟地址空间分给进程本身的是3GB(高址的1GB是内核空间,也就是0xc000000以上地址),所以有这样一个程序来测试用malloc最多能申请多少内存?

#include<stdio.h>
#include
unsigned max=0;
int main(void)
{
  int i,count;
  unsigned size[]={1024*1024,1024,1};
  for(i=0;i<3;i++){
    for(count=1;;count++){
      void *p=malloc(max+count*size[i]);
      if(p){
        max+=count*size[i];
        free(p);
      }
      else{
        break;
      }
    }
  }
  printf("The max memory i can alloc is %u\n",max);
  return 0;
}

在我的linux机器上跑了后发现只有1.1G左右的空间,想一下应该是内存768M+swap400多M,所以到不了那么大吧!

 

 

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter