Program Memory Usage from 'C'

Ron Kuris ron at rdk386.uucp
Tue Dec 19 17:32:42 AEST 1989


In article <601 at h.cs.wvu.wvnet.edu> dymm at b.cs.wvu.wvnet.edu (David Dymm) writes:
>
>I want to know [...] how much memory has been allocated by a 'C' program.
>
>I tried using "ps"...
>
>   sprintf (buffer, "ps -auxw | grep %d | grep -v grep > outfile", pid_number);
>   system (buffer);
>
>But this does NOT give consistent results...  Such as telling me
>that a program has allocated only 8 kb when I know it has allocated > 30kb.
This may be true.  You may only have 8kb of RESIDENT SET SIZE, or how
much core is actually in memory.  Based on the arguments to ps, it
looks like you're using BSD (or some variant thereof) which lists the
resident set size,

>Is there a better way??  Is there a direct 'C' cal that I can make
>to unix to give me the memory allocated on the stack??
The system call sbrk() should give you what you're looking for.  Save
the return value of sbrk(0) into some long variable when the program
starts, and then compare it with the value that it returns later.

If you have to have some external program monitor this, then you need
to look into some of the utility programs that may or may not be
available for your machine.  An example of one of these would be
'prss' for the sequent.  It gives everything you're looking for plus
more.  I don't think it's public domain (I can check if asked).  The
utility prints the total data, stack, and program both in core, and
on swap.  It also gives the shared data information.

It is one of several good performance tools available on the sequent.
/-----------------------------------\/\/\/\
| {unify|{sactoh0!siva}}!rdk386!ron | o o |
| Cut this out and present it to me |  -  |
| for a FREE $20.00 bill!  Expires  | awk |
| upon presentation.                |     |
\-----------------------------------/     \



More information about the Comp.unix.wizards mailing list