Failing to understand getrusage()

Yar Tikhiy yar at comp.chem.msu.su
Mon Mar 6 23:16:00 UTC 2006


On Thu, Mar 02, 2006 at 11:50:29PM +0000, Nick Barnes wrote:
> At 2006-03-02 22:24:17+0000, Nik Clayton writes:
> > I'm failing to understand how getrusage() works, which is a bit perplexing,
> > because it doesn't seem like it would be terribly complicated.
> 
> ru_maxrss is the maximum resident set size, not the heap size.
> malloc(big) doesn't grow the resident set.  Touching the memory you
> have allocated will grow the resident set.  Try this:
> 
>         getrusage(RUSAGE_SELF, &ru);
>         printf("%lu\n", ru.ru_maxrss);
>         p = malloc(SIZE);
>         assert(p)
>         getrusage(RUSAGE_SELF, &ru);
>         printf("%lu\n", ru.ru_maxrss);
>         for (i=0; i<SIZE; ++i) {
>                 p[i] = 0;
>         }
>         getrusage(RUSAGE_SELF, &ru);
>         printf("%lu\n", ru.ru_maxrss);

Well, there was a call to memset() in the original Nik's program
while your code just does the same by itself.

Personally, I'd like to say a "me too".  /me too fails to see why
in a quiet, idle system ru_maxrss is very unpredictable over numerous
runs of the test program, both before and after the malloc+memset.
Filling the memory with a non-zero value doesn't matter.  Is it the
Heizenberg daemon at work? :-)

-- 
Yar


More information about the freebsd-stable mailing list