Failing to understand getrusage()

Nik Clayton nik at
Thu Mar 2 22:25:10 UTC 2006

I'm failing to understand how getrusage() works, which is a bit perplexing,
because it doesn't seem like it would be terribly complicated.

I've attached the code.  My aim is to verify that I can use getrusage() to
do (admittedly crude) instrumentation of which functions in my program are
allocating lots of memory[1].  So I figure I can call getrusage() at various
points and look at the ru_maxrss member.

I'm confused because the results I'm getting don't make a great deal of
sense.  The attached program, when run 10 times one after the other,
produces the following output;

   before: 0, after: 1300
   before: 0, after: 0
   before: 0, after: 0
   before: 188, after: 188
   before: 0, after: 0
   before: 452, after: 452
   before: 0, after: 0
   before: 0, after: 1316
   before: 0, after: 0
   before: 0, after: 0

'before' is ru_maxrss before 'malloc(1024 * 1024)', 'after' is ru_maxrss 
afterwards.  Different runs produce slightly different numbers -- the point 
is that they vary widely, and in some cases are '0' for both cases.

Those results don't look sane to me.  From doing some googling it looks like 
there may be a delay between the process's resident set size increasing and 
the stats that getrusage() uses being updated.

If that's the case, can I (portably?) wait for the getrusage() stats to 
synchronise before I read them?  If it's not the case, what am I doing 
wrong?  Is there another (portable) mechanism I use to find out how big my 
process is?


[1] I'm actually instrumenting Perl applications, using Perl's built-in 
debugger, instrumenting at the Perl level, so solutions of the form "Use 
<this> profiling application" aren't going to be applicable.  I'm using 
BSD::Resource, which wraps getrusage(), and odd results from that led me to 
this C test.
-------------- next part --------------
/* Call getrusage() before and after allocating 1MB of RAM, and see what
   the maxrss figure is */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

#include <stdio.h>

	struct rusage  ru;
	char          *ptr;
	int            rc;
	rc = getrusage(RUSAGE_SELF, &ru);
	if(rc == -1) {
		perror("getrusage(): ");
		return 1;

	printf("before: %ld, ", ru.ru_maxrss);

	ptr = malloc(1024 * 1024);
	if(ptr == NULL) {
		perror("malloc(): ");
		return 1;
	memset(ptr, 0, 1024 * 1024); /* Zero-fill the memory */

	rc = getrusage(RUSAGE_SELF, &ru);
	if(rc == -1) {
		perror("getrusage(): ");
		return 1;

	printf("after: %ld\n", ru.ru_maxrss);


	return 0;

More information about the freebsd-stable mailing list