Measuring memory footprint in C/C++ code on FreeBSD

Razmig K strontium90 at
Fri Oct 21 08:44:26 UTC 2011

Le 20.10.2011 19:57, Razmig K a écrit :
> the memory footprint in C/C++ code for a program running under FreeBSD
> and Linux in terms of total process size including heap objects

     Well getrusage does actually exist in Linux, but its behaviour 
isn't the same as on FreeBSD; struct rusage memory usage information 
fields ru_ixrss (shared text), ru_idrss (unshared data) and ru_isrss 
(unshared stack) are always zero.
     To test and understand the behaviour of getrusage on FreeBSD, I 
have written a small C++ program that allocates several large double 
arrays on the stack as well as on the heap with few loops to delay 
execution, and tried using the aforementioned fields, in addition to 
ru_maxrss (max resident size) to deduce memory usage information of the 
program as reported by top, namely fields RES and SIZE, but somehow I 
didn't manage to get a result matching that of the latter.
     First of all, I'm not sure if I interpret man getrusage correctly. 
For ru_ixrss, it says that it is an "integral" value indicating the 
amount of memory used by the text segment that was also shared among 
other processes, and is expressed in units of kilobytes * 
ticks-of-execution, where ticks are statistics clock ticks, the 
statistics clock having a frequency of sysconf(_SC_CLK_TCK) ticks per 
second. Does this imply that I need to divide ru_ixrss by 
sysconf(_SC_CLK_TCK) * a certain number of seconds? In that case, what 
do I use for the latter? ru_stime or ru_utime?
     I have observed that unlike ru_idrss, ru_ixrss and ru_isrss depend 
on execution time which contradicts the manpage, since all three fields 
are supposed to be specified in the same unit.
     Lastly, I don't see how to use getrusage to deduce information on 
the program's heap use. As I said in my first message, I need to measure 
the memory footprint in terms of total process size including heap objects.
     Thanks again for any tips.


More information about the freebsd-hackers mailing list