此文版权属于作者所有,任何人、媒体或者网站转载、借用都必须征得作者本人同意!
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 修改历史:
*
| 日期 | 修改人 | 内容 |
|---|---|---|
| 2010-4-6 | 创建初稿 |
*/
#include
#include
#include
#include
// 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
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 // 51