Calculating memory used by a program

Calculating memory used by a program

Post by Raghu V. Hud » Wed, 11 Sep 1991 00:58:59



I am interested to compute the total memory used by a program.
I tried to use the getrusage function, but had some problems.

The man page for getrusage says :

  The getrusage subroutine returns information describing the
  resources utilized by the current process, or  all its terminated
  child processes.

However, the resources seem to be the same even after I allocate 1 MB
dynamically. What am I doing wrong in calculating the memory resource
used by my program? My program and the output follow.
Also, is there a function other than getrusage that can be used for this
purpose?

 Raghu V. Hudli
 IBM Corp.
===========================================================================
Disclaimer: The opinions expressed in this note are my own and do not
necessarily reflect those of my employer

===============================   program =================================

#include <sys/resource.h>

void dumprusage();
main(){
        int i;
        int *j;

        printf("i ? ");
        scanf("%d",&i);
        printf("resources before memory allocation\n");
        dumprusage();
        j = malloc(i*1024);
        printf("resources after memory allocation\n");
        dumprusage();

Quote:}

void dumprusage() {

        int who = RUSAGE_SELF;
        struct rusage Rusage;

       getrusage(who,&Rusage);

       printf("ru_utime %d\n",Rusage.ru_utime.tv_usec);
       printf("ru_stime %d\n",Rusage.ru_stime.tv_usec);

       printf("ru_maxrss %d\n",Rusage.ru_maxrss);
       printf("ru_ixrss %d\n",Rusage.ru_ixrss);
       printf("ru_idrss %d\n",Rusage.ru_idrss);
       printf("ru_isrss %d\n",Rusage.ru_isrss);

       printf("inputs %d\n",Rusage.ru_inblock);
       printf("outputs %d\n",Rusage.ru_oublock);

Quote:}

===============================   output  =================================

i ? 1024
resources before memory allocation
ru_utime 90000
ru_stime 160000
ru_maxrss 59
ru_ixrss 360
ru_idrss 156
ru_isrss 0
inputs 0
outputs 0
resources after memory allocation
ru_utime 90000
ru_stime 160000
ru_maxrss 59
ru_ixrss 360
ru_idrss 156
ru_isrss 0
inputs 0
outputs 0

 
 
 

Calculating memory used by a program

Post by Michael Bur » Thu, 12 Sep 1991 21:46:32



|>
|>
|> I am interested to compute the total memory used by a program.
|> I tried to use the getrusage function, but had some problems.
|>
|> The man page for getrusage says :
|>
|>   The getrusage subroutine returns information describing the
|>   resources utilized by the current process, or  all its terminated
|>   child processes.
|>
|>
|> However, the resources seem to be the same even after I allocate 1 MB
|> dynamically. What am I doing wrong in calculating the memory resource
|> used by my program? My program and the output follow.
|> Also, is there a function other than getrusage that can be used for this
|> purpose?
|>
|>  Raghu V. Hudli
|>  IBM Corp.

   the rest deleted ...

I think your problem is that you did not actually *USE* the memory
you reserved with malloc.  The AIX heap is sparse, meaning that it does
not actually assign the pages until you touch them (access them).
Try adding a loop after the malloc to touch every 512th byte or so
and see if that doesn't fix the problem.

--

Mgr. AIX Host Development     PO Box 200075             (512)258-5171 x3264
                              Austin, TX 78720-0075
Optimism: That quality that allows a tea kettle to sing
        though up to its nose in hot water.