程序:

写个小程序,测试下linux下一个进程可以大概分配多少内存

运行结果:

写个小程序,测试下linux下一个进程可以大概分配多少内存

 

疑问:

写个小程序,测试下linux下一个进程可以大概分配多少内存

虚拟机的物理内存是1G, 但是可以申请1.7G的堆内存,这真是个奇妙?后续研究一下

https://www.nowcoder.com/discuss/20610?type=0&order=0&pos=6&page=1

 

代码:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int cnt = 0;
    void* tmp = NULL;
    while (true)
    {
        cnt += 100;
        tmp = malloc(cnt * 1024 * 1024);
        if (tmp)
        {
            printf("malloc %d M ok.\n", cnt);
            free(tmp);
        }
        else
        {
            printf("malloc %d M failed.\n", cnt);
            break;
        }
    }
    printf("the test is over.\n");
}

 

 

相关文章: