| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1058 人关注过本帖
标题:[讨论]test how big is the heap memory
只看楼主 加入收藏
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:9 
[讨论]test how big is the heap memory

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;
}

搜索更多相关主题的帖子: heap memory test big 讨论 
2007-06-22 10:15
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
要看懂这段代码还真费力!

你能说说你的思路吗?
还有,我机器上不能运行,会出错。


还有,你一直吃内存,但是好象没有释放吧?

Fight  to win  or  die...
2007-06-22 11:00
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 

0. you may need to disable virtual memory for the system first;

1. eat_memory() eats memory (that block of memory is lost) and never releases it. That is the desgin. When you exhaust the available heap memory, any call to new() will fail; once the new() fails, the program will call the global new_handler (in our case, it is get_memory()). In our customized verion of the new handler (c++ has its own built-in/default new handler), we output information.

2. The source code compiles and runs on my windows xp pro (En) / VS 2005 system.


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-22 11:40
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
When I first compiles the programe,it takes about more than 10 seconds to finish,but the second time only takes less than 1 second.and at that flash my computer' cpu reach 99%;then memory released;test many times,a exception comes up,only a time.

Compile environment:DEV C ++

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-06-22 12:55
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

[QUOTE]1. eat_memory() eats memory (that block of memory is lost) and never releases it.[/QUOTE]
But it is not that condition on my computer ,I don't konw why and I want to make it.

continue...

[此贴子已经被作者于2007-6-22 17:09:06编辑过]


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-06-22 13:02
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 

1. The following file simulates the process of new() --- if you never overloaded new() before.

2. In my ogrinal post, I saved one block of memory at the beginning of main(), then I released that saved block to the system in get_memory(), so that the get_memory() can get more memory.

After the call to get_memory(), this statement in eat_memory()

int *p = new int[size]; // allocate memory

can run once more (and only 1 time) and then go to the "else" branch.

3. Don't know why it cannot run on your guys' system.


/*---------------------------------------------------------------------------
File name: new_handler.cpp
Author: HJin
Created on: 6/21/2007 23:57:40

Modification history:


Sample output:


my handler here

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Press any key to continue . . .

*/

#include <iostream>
using namespace std;

void myHandler()
{
cout<<"my handler here\n";

terminate(); // or abort()
}

/**
This overload of new() is excerpted from Scott Meyer's
book: Effective C++, which simulates what is going on
for when you say

int * p = new int[size]

in C++.

*/
void* operator new(size_t size) throw (std::bad_alloc)
{
/**
the change for size from 0 to 1 makes any object has
size >=1.

For exmample, if you have an empty class A:

class A
{
};

And you say

A* obj = new A;

Then obj has size >=1.
*/
if(size==0)
size=1;

// infinite loop: the system must make more
// memory available, otherwise it throws a bad_alloc
// exception.
while(1)
{
void *p = 0;
p = malloc(size);
if(p != 0) return p;

// get current new handler
new_handler globalHandler = set_new_handler(0);
// set the new handler
set_new_handler(globalHandler);

// if it is a valid handler, run it.
// In general, the global new handler should make
// more memory availabe from the system.
if(globalHandler)
{
(*globalHandler)();
}
else
{
throw(std::bad_alloc("bad alloc here."));
}
}
}

int main(int argc, char** argv)
{
new_handler oldHandler = set_new_handler(myHandler);

try
{
while(1)
{
int *p = new int[1024];
}
}
// no exception will be caught, since the
// new handler terminates the whole program
catch(const std::bad_alloc& e)
{
cout<<e.what()<<endl;
}
catch(const std::exception& e)
{
cout<<e.what()<<endl;
}

return 0;
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-22 15:17
I喜欢c
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:64
帖 子:1749
专家分:0
注 册:2007-3-2
收藏
得分:0 


好痛苦的交流

 我是指针,却丢失了目标地址!          我是循环,却缺少了结束条件!      我是函数,却没有人来调用!   
2007-06-22 18:10
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
不懂了,呵呵,HJin,还是搞个中文的系统吧,或者装个什么软件可以写汉字!

呵呵~

Fight  to win  or  die...
2007-06-22 20:52
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
The thing is I am using a corporate laptop --- yes, I am one of the adminstrators; and no, I cannot change the system configuration without the approval from the head of the IT department.

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-22 23:19
游乐园
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:671
专家分:0
注 册:2006-11-1
收藏
得分:0 
pretty good ...

btw,Mr. HJin,what's your profession now?
It seems that you are full of experience in using C++.Every program u wrote was with adequate explaination and pre-analysis.
Anyhow,plz go on with your post and idea which do benefit us(under graduate students) a lot.
I'd like to catch up with the pace here in my idle online time...

unicorn-h.spaces. ◇◆ sava-scratch.spaces.
2007-06-24 20:31
快速回复:[讨论]test how big is the heap memory
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019286 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved