目录
32位空间分布
64位空间分布
一、Windows下可以申请多少内存
32位下可以申请多少内存
#include <iostream>
#define MAX_SIZE 1024*1024*1024
#define MAX_MEMORY 4
int main()
{
char* addr[MAX_MEMORY];
for (int i = 0; i < MAX_MEMORY; ++i)
{
addr[i] = (char*)malloc(MAX_SIZE);
if (addr[i] == nullptr)
{
perror("malloc failed!\n");
return -1;
}
printf("第%d次申请内存,malloc 1GB success! 内存首地址:%x\n", i+1, addr[i]);
}
getchar();
return 0;
}
64位下可以申请多少内存
#include <iostream>
#define MAX_SIZE 1024*1024*1024
#define MAX_MEMORY 30
int main()
{
char* addr[MAX_MEMORY];
for (int i = 0; i < MAX_MEMORY; ++i)
{
addr[i] = (char*)malloc(MAX_SIZE);
if (addr[i] == nullptr)
{
perror("malloc failed!\n");
return -1;
}
printf("第%d次申请内存,malloc 1GB success! 内存首地址:%x\n", i+1, addr[i]);
}
getchar();
return 0;
}
总结:
- malloc申请的是虚拟内存
- 多次Windows32位下测试只能申请空间小于2GB
- 多次Windows64下测试申请18GB-19GB
二、Windows下可以申请多少内存
物理内存2GB,交换区4GB
内核版本3.10,64位操作系统
申请到127TB就申请不了了,因为程序运行还需要空间。