tags:

views:

60

answers:

4

Is there a libc function (or equivalent) to know the current size of the heap?

I have a memory problem in my application, and it seems being able to monitor the heap when I want to would help me find the problem. So is there a way to know the current size of the heap?

+4  A: 

No.

As the functionality you want is for debugging, it would make a lot more sense for you to use your debugger or your operating system's resource accounting to monitor the process's memory usage, instead of trying to code that into your program.

If you really want your program to keep track of its own memory usage, the only portable way to do this is to avoid using malloc and free directly and instead call them through wrappers that increment/decrement a counter. This will not account for memory fragmentation, but if your interest is in the logical memory usage of your program and not the impact on physical resources, a counter implemented this way might actually be more informative than looking at the operating system's resource accounting.

If you only care about a particular target platform or family of platforms, there may also be functions above and beyond the C standard which do what you want. On POSIX, lookup getrusage.

R..
+2  A: 

What you need is http://valgrind.org/

Habbie
A: 

Still wouldn't help. Do you want to know :
The total address space.
The space available to user programs
The space unallocated, to this process, including swap or not
The biggest free chunk available

etc.

Martin Beckett
A: 

You can use tools like Purify to debug memory issues. This article from IBM contains a lot of details about the sources of such problems and pointers to solutions.

Aaron Digulla