32 位模式下 C/C++ 程序到底可以用多少内存
此文版权属于作者所有,任何人、媒体或者网站转载、借用都必须征得作者本人同意!
32 位的程序寻址空间是 4G,因此能用的内存应该有 4G,除掉一些系统等使用的乱七八糟的东西,3G 内存应该没有问题吧,这些只是猜测,写个程序测一下,结果如下:
测试结论:
1. 栈内存最大可用 768k 左右;
2. 堆内存最大可用 1.586G 左右。
结果令人沮丧,才 1.6 G!!!
测试环境:Windows7 2G、IBMSL300、VC9
附录测试程序等
测试程序只考虑 windows,首先,说明一下相关知识:
内存可以来自两个地方,在栈空间上分配的内存(栈内存)和在堆空间分配的内存(堆内存)。
栈内存,通过 alloca 来申请。
堆内存,通过 malloc、new、VirtualAlloc 来申请。
测试程序如下:
/* $Id$ */
/**
* /file memory_test.cpp
*
* /brief 大内存申请测试
*
* /version $Rev$
* /author
* /date 2010年04月06日:09:24
*
* /note 修改历史:<br>
* <table>
* <tr><th>日期</th><th>修改人</th><th>内容</th></tr>
* <tr><td>2010-4-6</td><td></td><td>创建初稿</td>
* </tr>
* </table>
*/
#include <stdio.h>
#include <malloc.h>
#include <windows.h>
#include <excpt.h>
// alloca: Allocates memory on the stack
static bool alloca_test(size_t s)
{
__try{
alloca(s); printf(" √1 alloca(%u) 通过/n", s);
}__except( GetExceptionCode() == STATUS_STACK_OVERFLOW ){
printf(" ×1 alloca(%u) 失败,内存溢出/n", s);
_resetstkoflw();
return false;
}
__try{
alloca(s); printf(" √2 alloca(%u) 通过/n", s);
}__except( GetExceptionCode() == STATUS_STACK_OVERFLOW ){
printf(" ×2 alloca(%u) 失败,内存溢出/n", s);
_resetstkoflw();
return false;
}
return true;
}
// malloc: Allocates memory blocks.
static bool malloc_test(size_t s)
{
void* buf = malloc(s);
if (buf)
printf(" √malloc(%u) 通过/n", s);
else
printf(" ×malloc(%u) 失败/n", s);
free(buf);
return buf != 0;
}
// malloc: Allocates memory blocks.
static bool malloc_test2(size_t s)
{
void* buf1 = malloc(s);
void* buf2 = malloc(s);
if (buf1)
printf(" √buf1 malloc(%u) 通过/n", s);
else
printf(" ×buf1 malloc(%u) 失败/n", s);
if (buf2)
printf(" √buf2 malloc(%u) 通过/n", s);
else
printf(" ×buf2 malloc(%u) 失败/n", s);
free(buf1);
free(buf2);
return buf1 != 0 && buf2 != 0;
}
static bool new_test(size_t s)
{
try
{
char* buf = new char[s];
delete[] buf;
printf(" √new char[%u] 成功/n", s);
return true;
}catch(...){
printf(" ×new char[%u] 失败/n", s);
return false;
}
}
bool VirtualAlloc_test(size_t s)
{
LPVOID buf = VirtualAlloc(NULL, s, MEM_COMMIT, PAGE_READWRITE);
VirtualFree(buf, 0, MEM_RELEASE);
if (buf)
printf(" √VirtualAlloc(NULL, %u, ..) 成功/n", s);
else
printf(" ×VirtualAlloc(NULL, %u, ..) 失败/n", s);
return buf != 0;
}
bool VirtualAlloc_test2(size_t s)
{
LPVOID buf1 = VirtualAlloc(NULL, s, MEM_COMMIT, PAGE_READWRITE);
LPVOID buf2 = VirtualAlloc(NULL, s, MEM_COMMIT, PAGE_READWRITE);
VirtualFree(buf1, 0, MEM_RELEASE);
VirtualFree(buf2, 0, MEM_RELEASE);
if (buf1)
printf(" √1 VirtualAlloc(NULL, %u, ..) 成功/n", s);
else
printf(" ×1 VirtualAlloc(NULL, %u, ..) 失败/n", s);
if (buf2)
printf(" √2 VirtualAlloc(NULL, %u, ..) 成功/n", s);
else
printf(" ×2 VirtualAlloc(NULL, %u, ..) 失败/n", s);
return buf1 != 0 && buf2 != 0;
}
const size_t SIZE_K = (((size_t)1) << 10);
const size_t SIZE_M = (SIZE_K << 10);
const size_t SIZE_G = (SIZE_M << 10);
// 把大小转换为xxxK、xxxM、xxxG 之类的描述,非线程安全!
static char const*const i2a(size_t s)
{
static char buf[24];
if (s < SIZE_M) {
sprintf(buf, "%uK", s/SIZE_K);
} else if (s < SIZE_G) {
sprintf(buf, "%.3fM", (double)s/SIZE_M);
} else {
sprintf(buf, "%.3fG", (double)s/SIZE_G);
}
return buf;
}
int main(int, char**)
{
size_t sizes[] =
{
SIZE_K // 1K
, 128*SIZE_K // 128K
, 256*SIZE_K // 256K
, 512*SIZE_K // 512K
, 768*SIZE_K // 768K
, SIZE_M // 1M
, 512*SIZE_M // 512M
, 640*SIZE_M // 640M
, 768*SIZE_M // 768M
, SIZE_G // 1G
, SIZE_G + 100*SIZE_M // 1.1G
, SIZE_G + 200*SIZE_M // 1.2G
, SIZE_G + 300*SIZE_M // 1.3G
, SIZE_G + 400*SIZE_M // 1.4G
, SIZE_G + 500*SIZE_M // 1.5G
, SIZE_G + 600*SIZE_M // 1.6G
, SIZE_G + 700*SIZE_M // 1.7G
, SIZE_G + 800*SIZE_M // 1.8G
, SIZE_G + 900*SIZE_M // 1.9G
};
printf("大内存申请测试/n");
bool res = true;
for (size_t i = 0, c = sizeof(sizes)/sizeof(sizes[0]); i != c && res; ++i)
{
size_t s = sizes[i];
printf(" 测试申请%s 内存/n", i2a(s));
res = false;
res = alloca_test(s) || res;
res = malloc_test(s) || res;
res = new_test(s) || res;
res = VirtualAlloc_test(s) || res;
if (s >= 1024*1024*512)
{
res = malloc_test2(s) || res;
res = VirtualAlloc_test2(s) || res;
}
}
return 0;
}
运行结果如下:
大内存申请测试
测试申请 1K 内存
√ 1 alloca(1024) 通过
√ 2 alloca(1024) 通过
√ malloc(1024) 通过
√ new char[1024] 成功
√ VirtualAlloc(NULL, 1024, ..) 成功
测试申请 128K 内存
√ 1 alloca(131072) 通过
√ 2 alloca(131072) 通过
√ malloc(131072) 通过
√ new char[131072] 成功
√ VirtualAlloc(NULL, 131072, ..) 成功
测试申请 256K 内存
√ 1 alloca(262144) 通过
√ 2 alloca(262144) 通过
√ malloc(262144) 通过
√ new char[262144] 成功
√ VirtualAlloc(NULL, 262144, ..) 成功
测试申请 512K 内存
√ 1 alloca(524288) 通过
× 2 alloca(524288) 失败,内存溢出
√ malloc(524288) 通过
√ new char[524288] 成功
√ VirtualAlloc(NULL, 524288, ..) 成功
测试申请 768K 内存
√ 1 alloca(786432) 通过
× 2 alloca(786432) 失败,内存溢出
√ malloc(786432) 通过
√ new char[786432] 成功
√ VirtualAlloc(NULL, 786432, ..) 成功
测试申请 1.000M 内存
× 1 alloca(1048576) 失败,内存溢出
√ malloc(1048576) 通过
√ new char[1048576] 成功
√ VirtualAlloc(NULL, 1048576, ..) 成功
测试申请 512.000M 内存
× 1 alloca(536870912) 失败,内存溢出
√ malloc(536870912) 通过
√ new char[536870912] 成功
√ VirtualAlloc(NULL, 536870912, ..) 成功
√ buf1 malloc(536870912) 通过
√ buf2 malloc(536870912) 通过
√ 1 VirtualAlloc(NULL, 536870912, ..) 成功
√ 2 VirtualAlloc(NULL, 536870912, ..) 成功
测试申请 640.000M 内存
× 1 alloca(671088640) 失败,内存溢出
√ malloc(671088640) 通过
√ new char[671088640] 成功
√ VirtualAlloc(NULL, 671088640, ..) 成功
√ buf1 malloc(671088640) 通过
√ buf2 malloc(671088640) 通过
√ 1 VirtualAlloc(NULL, 671088640, ..) 成功
√ 2 VirtualAlloc(NULL, 671088640, ..) 成功
测试申请 768.000M 内存
× 1 alloca(805306368) 失败,内存溢出
√ malloc(805306368) 通过
√ new char[805306368] 成功
√ VirtualAlloc(NULL, 805306368, ..) 成功
√ buf1 malloc(805306368) 通过
√ buf2 malloc(805306368) 通过
√ 1 VirtualAlloc(NULL, 805306368, ..) 成功
√ 2 VirtualAlloc(NULL, 805306368, ..) 成功
测试申请 1.000G 内存
× 1 alloca(1073741824) 失败,内存溢出
√ malloc(1073741824) 通过
√ new char[1073741824] 成功
√ VirtualAlloc(NULL, 1073741824, ..) 成功
√ buf1 malloc(1073741824) 通过
× buf2 malloc(1073741824) 失败
√ 1 VirtualAlloc(NULL, 1073741824, ..) 成功
× 2 VirtualAlloc(NULL, 1073741824, ..) 失败
测试申请 1.098G 内存
× 1 alloca(1178599424) 失败,内存溢出
√ malloc(1178599424) 通过
√ new char[1178599424] 成功
√ VirtualAlloc(NULL, 1178599424, ..) 成功
√ buf1 malloc(1178599424) 通过
× buf2 malloc(1178599424) 失败
√ 1 VirtualAlloc(NULL, 1178599424, ..) 成功
× 2 VirtualAlloc(NULL, 1178599424, ..) 失败
测试申请 1.195G 内存
× 1 alloca(1283457024) 失败,内存溢出
√ malloc(1283457024) 通过
√ new char[1283457024] 成功
√ VirtualAlloc(NULL, 1283457024, ..) 成功
√ buf1 malloc(1283457024) 通过
× buf2 malloc(1283457024) 失败
√ 1 VirtualAlloc(NULL, 1283457024, ..) 成功
× 2 VirtualAlloc(NULL, 1283457024, ..) 失败
测试申请 1.293G 内存
× 1 alloca(1388314624) 失败,内存溢出
√ malloc(1388314624) 通过
√ new char[1388314624] 成功
√ VirtualAlloc(NULL, 1388314624, ..) 成功
√ buf1 malloc(1388314624) 通过
× buf2 malloc(1388314624) 失败
√ 1 VirtualAlloc(NULL, 1388314624, ..) 成功
× 2 VirtualAlloc(NULL, 1388314624, ..) 失败
测试申请 1.391G 内存
× 1 alloca(1493172224) 失败,内存溢出
√ malloc(1493172224) 通过
√ new char[1493172224] 成功
√ VirtualAlloc(NULL, 1493172224, ..) 成功
√ buf1 malloc(1493172224) 通过
× buf2 malloc(1493172224) 失败
√ 1 VirtualAlloc(NULL, 1493172224, ..) 成功
× 2 VirtualAlloc(NULL, 1493172224, ..) 失败
测试申请 1.488G 内存
× 1 alloca(1598029824) 失败,内存溢出
√ malloc(1598029824) 通过
√ new char[1598029824] 成功
√ VirtualAlloc(NULL, 1598029824, ..) 成功
√ buf1 malloc(1598029824) 通过
× buf2 malloc(1598029824) 失败
√ 1 VirtualAlloc(NULL, 1598029824, ..) 成功
× 2 VirtualAlloc(NULL, 1598029824, ..) 失败
测试申请 1.586G 内存
× 1 alloca(1702887424) 失败,内存溢出
√ malloc(1702887424) 通过
√ new char[1702887424] 成功
√ VirtualAlloc(NULL, 1702887424, ..) 成功
√ buf1 malloc(1702887424) 通过
× buf2 malloc(1702887424) 失败
√ 1 VirtualAlloc(NULL, 1702887424, ..) 成功
× 2 VirtualAlloc(NULL, 1702887424, ..) 失败
测试申请 1.684G 内存
× 1 alloca(1807745024) 失败,内存溢出
× malloc(1807745024) 失败
× new char[1807745024] 失败
× VirtualAlloc(NULL, 1807745024, ..) 失败
× buf1 malloc(1807745024) 失败
× buf2 malloc(1807745024) 失败
× 1 VirtualAlloc(NULL, 1807745024, ..) 失败
× 2 VirtualAlloc(NULL, 1807745024,