公司有台32位的win server 2003服务器,但开发的程序在峰值情况下确实超过2G内存的占用(已在其它64位机器上测试过)。程序在win server 2003机器上跑总会出现CMemoryException后僵死。为此查阅了一些资料,目前看来最有用的信息是:
http://support.microsoft.com/kb/823440/zh-cn (在基于 Windows Server 2003 的系统上的 Exchange Server 2003 中使用 /3GB 开关)
按照文章中的描述,修改了boot.ini后,为使程序能使用更多内存,还需要修改程序的链接选项:
配置属性-链接器-系统-启用大地址
// Win32MemUsage.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <malloc.h>
#include <stdio.h>
int test_mem_use()
{
size_t block_size = 1024 * 1024 *10; //10M
int i = 0;
int total = 0;
while (1)
{
void * p = malloc (block_size);
if (!p)
break;
++i;
total += 10;
printf ("%d, %dM\n", i, total);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
test_mem_use();
return 0;
}
当没有设置大地址时,运行程序最后输出是:
201, 2010M
当设置大地址后,运行程序最后输出是:
298, 2980M
OK,确实看到效果了!