(1)
Use GNU time command
Please check if your systems has GNU
time installed (e.g., in /usr/bin/time). Note that this is not the
built-in command in a shell.
A short synopsis of time
(all followings are copied from time's man page):
Disregarding the name of the utility, GNU makes it output lots of
useful information, not only about time used, but also on other
resources like memory, I/O and IPC calls (where available). The
output is formatted using a format string that can be specified
using the -f option or the TIME environment variable.
The default format string is
%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k
%Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps
Partial formats in Memory section:
%M Maximum resident set size of the process during its lifetime, in Kbytes.
%t (Not in tcsh.) Average resident set size of the process, in Kbytes.
%K Average total (data+stack+text) memory use of the process, in Kbytes.
(2) Another simple but manual solution: stop your program within a debugger and watch for the memory usage.
(3) You can use the following trick in your program, but they are system-dependent:
a. Solaris: use getrusage system call (check manual of getrusage by "man").
b. Linux: cat /proc/$PID/statm ($PID is your current process ID).