2013-05-04 wcdj
向青年节致敬!
问题:堆空间最大可以申请多少呢?
测试:
#include <iostream>
#include <cstdlib>
#include <new>
void no_memory () {
std::cout << "Failed to allocate memory!\n";
std::exit (1);
}
unsigned long size = 2*1024*1024*1024ul + 1024*1024*700;
int main () {
std::set_new_handler(no_memory);
std::cout << "attempting to allocate " << size << "... ";
char* p = new char [size];
memset(p, 0x01, size);
std::cout << "ok\n";
sleep(20);
delete[] p;
return 0;
}
结论:
(1) 对于Linux
For x86 CPUs, Linux allows split the user and kernel address ranges in differents ways: 3G/1G user/kernel (default) , 1G/3G user/kernel or 2G/2G user/kernel.
(2) 对于Windows
On a 32-bit Microsoft Windows installation, by default, only 2 GiB are made available to processes for their own use. The other 2GB are used by the operating system. On later 32-bit editions of Microsoft Windows it is possible to extend the user-mode virtual address space to 3 GiB while only 1 GiB is left for kernel-mode virtual address space by marking the programs as IMAGE_FILE_LARGE_ADDRESS_AWARE and enabling the /3GB switch in the boot.ini file.
https://msdn.microsoft.com/en-us/library/aa366912(VS.85).aspx