473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How can I determine the size of the heap

Pep
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

The C++ code is running on linux and freeBSD.

The actual library I am interfacing with uses malloc and i have no
control over it so I need to see the state of the heap after each use
of the libraries allocation functions.

TIA.

Aug 16 '06 #1
10 3001
Pep wrote:
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

The C++ code is running on linux and freeBSD.

The actual library I am interfacing with uses malloc and i have no
control over it so I need to see the state of the heap after each use
of the libraries allocation functions.
Not in standard C++, you could roll your own new/delete and keep stats.
You could also track the memory allocated to the process.

--
Ian Collins.
Aug 16 '06 #2
Pep

Ian Collins wrote:
Pep wrote:
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

The C++ code is running on linux and freeBSD.

The actual library I am interfacing with uses malloc and i have no
control over it so I need to see the state of the heap after each use
of the libraries allocation functions.
Not in standard C++, you could roll your own new/delete and keep stats.
You could also track the memory allocated to the process.

--
Ian Collins.
As I thought :(

Aug 16 '06 #3

Pep wrote:
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?
No, because 1. there may not be such a size and 2. even if there were,
there is no guarantee that any change to it is due to actions of the
program
itself.

E.g. if "the heap" is managed directly by the OS, its size might change
due
to other programs, and it might use lazy bookkeeping. (lazy: when
memory is
released, it's put on a quick-recycle list instead of being marked as
free in the
heap.)

Michiel.

Aug 16 '06 #4
Ian Collins wrote:
Pep wrote:
>I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

The C++ code is running on linux and freeBSD.

The actual library I am interfacing with uses malloc and i have no
control over it so I need to see the state of the heap after each use
of the libraries allocation functions.
Not in standard C++, you could roll your own new/delete and keep stats.
You could also track the memory allocated to the process.
And that still doesn't help you much if you have things that allocate
memory outside of new/delete. You need help outside the C++ language.
If all you want to know is the heap size, most UNIX variants use a call
to sbrk() that can be used to probe where the heap is. Many better
tools are also available. Google: FreeBSD Heap Debugging
Aug 16 '06 #5
Pep

Mi*************@tomtom.com wrote:
Pep wrote:
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

No, because 1. there may not be such a size and 2. even if there were,
there is no guarantee that any change to it is due to actions of the
program
itself.

E.g. if "the heap" is managed directly by the OS, its size might change
due
to other programs, and it might use lazy bookkeeping. (lazy: when
memory is
released, it's put on a quick-recycle list instead of being marked as
free in the
heap.)

Michiel.
Thanks for the explanation. I was just whistling in the wind for a
quick tool to help work out what was going on, as it turns out now I
have been given more project time to deprecate the old C library and
replace with a good C++ library which will be using new/delete and
exceptions :)

Aug 16 '06 #6
Pep

Ron Natalie wrote:
Ian Collins wrote:
Pep wrote:
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

The C++ code is running on linux and freeBSD.

The actual library I am interfacing with uses malloc and i have no
control over it so I need to see the state of the heap after each use
of the libraries allocation functions.
Not in standard C++, you could roll your own new/delete and keep stats.
You could also track the memory allocated to the process.

And that still doesn't help you much if you have things that allocate
memory outside of new/delete. You need help outside the C++ language.
If all you want to know is the heap size, most UNIX variants use a call
to sbrk() that can be used to probe where the heap is. Many better
tools are also available. Google: FreeBSD Heap Debugging
I see your point and thanks for the info. I will be replacing the old
C code for better C++ code.

Aug 16 '06 #7
In article <11**********************@h48g2000cwc.googlegroups .com>,
Pep <pe**********@yahoo.co.ukwrote:
>Mi*************@tomtom.com wrote:
>Pep wrote:
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

No, because 1. there may not be such a size and 2. even if there were,
there is no guarantee that any change to it is due to actions of the
program
itself.

E.g. if "the heap" is managed directly by the OS, its size might change
due
to other programs, and it might use lazy bookkeeping. (lazy: when
memory is
released, it's put on a quick-recycle list instead of being marked as
free in the
heap.)

Thanks for the explanation. I was just whistling in the wind for a
quick tool to help work out what was going on, as it turns out now I
have been given more project time to deprecate the old C library and
replace with a good C++ library which will be using new/delete and
exceptions :)
Don't go too hog wild. :) That is, using malloc/free or even
new/delete may be programming at the wrong level of abstraction,
so give it some deep thought, even if you are doing lib centric code,
especially if you are thinking of just replacing one with the other.
This statement is not to say that it won't work or that it's the
wrong thing, just that you may have better choices available to you,
and so "just" consider alternatives first.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 16 '06 #8
In article <11**********************@i3g2000cwc.googlegroups. com>,
Pep <pe**********@yahoo.co.ukwrote:
>
Ron Natalie wrote:
>Ian Collins wrote:
Pep wrote:
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

The C++ code is running on linux and freeBSD.

The actual library I am interfacing with uses malloc and i have no
control over it so I need to see the state of the heap after each use
of the libraries allocation functions.

Not in standard C++, you could roll your own new/delete and keep stats.
You could also track the memory allocated to the process.

And that still doesn't help you much if you have things that allocate
memory outside of new/delete. You need help outside the C++ language.
If all you want to know is the heap size, most UNIX variants use a call
to sbrk() that can be used to probe where the heap is. Many better
tools are also available. Google: FreeBSD Heap Debugging

I see your point and thanks for the info. I will be replacing the old
C code for better C++ code.
I'm unable to get to the beginning of this thread, but doing 1:1
replacements is not always necessarily better. As per my other
post, consider things in a more grander scale, otherwise you
may be piecemealing a mess. (And again, a mess doesn't have to
be the result, but just be cautious and know why you're changing
one thing for the other, and what any higher level implication can
and do and will mean if they are chosen instead).
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 16 '06 #9
Pep

Greg Comeau wrote:
In article <11**********************@i3g2000cwc.googlegroups. com>,
Pep <pe**********@yahoo.co.ukwrote:

Ron Natalie wrote:
Ian Collins wrote:
Pep wrote:
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

The C++ code is running on linux and freeBSD.

The actual library I am interfacing with uses malloc and i have no
control over it so I need to see the state of the heap after each use
of the libraries allocation functions.

Not in standard C++, you could roll your own new/delete and keep stats.
You could also track the memory allocated to the process.


And that still doesn't help you much if you have things that allocate
memory outside of new/delete. You need help outside the C++ language.
If all you want to know is the heap size, most UNIX variants use a call
to sbrk() that can be used to probe where the heap is. Many better
tools are also available. Google: FreeBSD Heap Debugging
I see your point and thanks for the info. I will be replacing the old
C code for better C++ code.

I'm unable to get to the beginning of this thread, but doing 1:1
replacements is not always necessarily better. As per my other
post, consider things in a more grander scale, otherwise you
may be piecemealing a mess. (And again, a mess doesn't have to
be the result, but just be cautious and know why you're changing
one thing for the other, and what any higher level implication can
and do and will mean if they are chosen instead).
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
I agree that replacement for the sake of it is not always the best
choice but in this case the old C library is not the best code and does
not perform even the most basic of validation for things like the
pointer being safe before it frees the memory or even that it got the
memory it asked for.

This is a critical library and has been traced as the root cause of a
number of problems in the code so it is best in this case to replace
rather than try to re-engineer it.

Aug 17 '06 #10
In article <11**********************@i3g2000cwc.googlegroups. com>,
Pep <pe**********@yahoo.co.ukwrote:
>Greg Comeau wrote:
>In article <11**********************@i3g2000cwc.googlegroups. com>,
Pep <pe**********@yahoo.co.ukwrote:
>Ron Natalie wrote:
Ian Collins wrote:
Pep wrote:
I have a problem I need to solve which looks like memory exhaustion.
Is there a function that I can use in c++ to determine the size of the
heap before and after a allocation?

The C++ code is running on linux and freeBSD.

The actual library I am interfacing with uses malloc and i have no
control over it so I need to see the state of the heap after each use
of the libraries allocation functions.

Not in standard C++, you could roll your own new/delete and keep stats.
You could also track the memory allocated to the process.

And that still doesn't help you much if you have things that allocate
memory outside of new/delete. You need help outside the C++ language.
If all you want to know is the heap size, most UNIX variants use a call
to sbrk() that can be used to probe where the heap is. Many better
tools are also available. Google: FreeBSD Heap Debugging

I see your point and thanks for the info. I will be replacing the old
C code for better C++ code.

I'm unable to get to the beginning of this thread, but doing 1:1
replacements is not always necessarily better. As per my other
post, consider things in a more grander scale, otherwise you
may be piecemealing a mess. (And again, a mess doesn't have to
be the result, but just be cautious and know why you're changing
one thing for the other, and what any higher level implication can
and do and will mean if they are chosen instead).

I agree that replacement for the sake of it is not always the best
choice but in this case the old C library is not the best code and does
not perform even the most basic of validation for things like the
pointer being safe before it frees the memory or even that it got the
memory it asked for.

This is a critical library and has been traced as the root cause of a
number of problems in the code so it is best in this case to replace
rather than try to re-engineer it.
I can't but help feeling that you are blaming the C library.
(I don't think you are, but it could sound that way.)
Although I agree there are limitations here, I think the operative
word in your post is re-enginnering because probably the _user code_
not the C lib itself was not written to deal with the tradeoffs of
the C lib. I am therefore left to conclude that although using
new/delete may bring things up a notch or two (nothing wrong with
that in and of itself at first glace) that any underlying problems
with the user code now will still exist upon such changes.
This is not such say some problems won't be uncovered in such a process,
just that it's probably at too low level a "fix" and usually the
result is that it won't possess the "magic" necessary in the grand
scheme of things. I am not saying not to try/do this, just that
I believe your underlying problem is greater than what I'm understanding
you want out of this fix, even sight unseen. Also, there may be
additional ways to reengineer the lib while still providing the
same interfaces, although that could be messy too.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 17 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark | last post by:
I am using JVM version 1.4.1 (java.vm.version=1.4.1-1) to run my program on UNIX. I use java command line option "java" to invoke my program. How will I know what the default heap size settings...
6
by: Dima | last post by:
How can I know size of avaible memory in heap? For example : .... .... // size = N cout << "Size of Heap = " << SizeOfHeap() << endl; int* i = new int; // size = N - sizeof(int) cout << "Size...
1
by: nrhayyal | last post by:
hi all, i am working on C++ on AIX machine. i am running one of my module which is built using 10+ user built libraries. i am getting St9bad_alloc exception. i came to know that this exception...
31
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
11
by: LordHog | last post by:
Hello, I recently wrote an application that is used for testing units in a burn-in chamber. It uses two external library that require the use of P\Invoke in order to work with them. There is a...
11
by: ganesh.kundapur | last post by:
Hi, Is there any way to get the total heap size allocated to a process in c on linux platform. Regards, Ganesh
5
by: sunny | last post by:
Hi All Is there any way to determine stack and heap size, during runtime. i.e can we predict stack overflow. etc
5
by: Paul | last post by:
hi, there, I was asked such a question: how to determine the size of memory of the int pointer pointed? for example int GetTheSizeofMemory(int *buffer) { int size; //enter your code here,...
2
by: shorti | last post by:
Hello, I am running DB2 V8.2 on AIX. I am looking for a way to get the memory size of a specific db2 agent. I have found the db2mtrk tool that gives me a list of all the applications and heap...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.