找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1595|回复: 0

[讨论]test how big is the heap memory

[复制链接]
发表于 2009-11-1 08:07:08 | 显示全部楼层 |阅读模式 IP:江苏扬州
Following is my simple test. If you have a better test algorithm,
please drop me a line.

Thanks,

HJin


/*---------------------------------------------------------------------------
File name: HeapMemoryTest.cpp
Author: HJin
Created on: 6/21/2007 19:05:18
Modification history:
We want to know how big the heap memory is, when
we use new or malloc to dynamically allocate memory.
My machine has 1Gb physical memory, so the heap memory
is less than 1Gb. My test shows that 3/4 of the physical
memory can be used for the heap.
Sample output:
heap addr = 00385008
heap exhausted
heap addr ends at 2d20d728
approximate heap size is 718.533 M
Inside eat_memory(). heap addr is 00385008
Press any key to continue . . .
*/
#include <iostream>
#include <new>
using namespace std;
const int BlockSize = 1024; // 1k
int *pSave;
long addrKeeper = 0L;
bool hasMoreMemory = true;
void get_memory()
{
cerr << "heap exhausted" << endl;
cout<<"heap addr ends at "<<std::hex<<addrKeeper<<endl;
cout<<"approximate heap size is "<<std::oct<<double(addrKeeper-(long)pSave)/(1024.0*1024.0)<<" M"<<endl;
delete [] pSave; // release saved block
hasMoreMemory = false;
}
void eat_memory(int size)
{
/**
when this allocation fails, the system calls the new_handler.
Once the new_handler returns, hasMoreMemory = false.
*/
int *p = new int[size]; // allocate memory
if (hasMoreMemory)
{
addrKeeper = (long)p;
eat_memory(size); // recursive call
}
else
cerr << "Inside eat_memory(). heap addr is " << p << endl;
}
int main()
{
set_new_handler(get_memory); // specify handler
pSave = new int[BlockSize]; // save one block from heap
cerr << "heap addr = " << pSave << endl;
eat_memory(BlockSize);
return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 19:21 , Processed in 0.266595 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表