In article <berdahl-32FAA6.email@example.com>,
Eric Berdahl <firstname.lastname@example.org> wrote:
I have a program which looks just great and seems to pass all
functionality tests. Among the things I need to do to qualify it for
shipping is to run a long term heap analysis on it.
That is, I want to start the program running with my automated testing
harness and watch the heap usage over the next few
hours/days/weeks/months to make sure there are no subtle memory leaks in
What you describe is one one of looking. We’ll deal with that one first
– there is another.
I thought that pidin might report heap usage somewhere, but I can’t find
the right information anywhere. I know pidin will give me code and data
usage for each library. I know that pidin will give me stack usage for
each thread. I do not see where it will give me heap usage for a given
As Igor says in his reply the mapped chunks are reported by pidin in the
data size for the program. Some of it is the static initialized (.data) and
static uninitialized data (.bss) for the program, so you would have to
factor out that, the corresponding data for all dynamic libraries and any
explicit mmap() requests.
There are also circumstances (reserved mmap() virtual address mappings
with no physical pages committed) that can cause this to be unreliable.
Okay, so it’s rare, but some programs do it.
Can someone suggest a way to get heap usage stats for a process?
You could factor out the data from pidin, or you could construct /proc
requests to find out the page mappings directly and analyze them.
However, if you are willing to modify the program somewhat, the malloc
library has some undocumented profiling features.
The _malloc_stats variable has low-level information about the
amount of data in use (blocks and bands), and the total heap as well
as user heap (the amount actually requested by the program). It is
of type “struct malloc_stats”, defined in malloc.h.
Basically, m_freemem tells the amount of available space in the free list,
while m_allocmem tells the amount of space taken up for big blocks in use.
Likewise, m_small_freemem tells the amount of space available in bands
(buckets for small allocations, ranging in size from 8-64 bytes), while
m_small_allocmem tells the amount of space in use by the program for
small blocks. In contrast m_heapsize gives the full size of the heap,
including internal fragmentation.
The other members give profiling information about the number of times
various operations have been performed.
Looking ahead, let’s say I determine that there is a memory leak in the
program. What tools/APIs would I use to instrument my code to look for
Read the tech note on heap analysis. It describes how the malloc_g
library can be used to detect heap data that is no longer referred
to by the program. To use it on a running program, you would
need some way to trigger a call to malloc_dump_unreferenced.
Thanks in advance,
Steve Furr email: email@example.com
QNX Software Systems, Ltd.