Consider I am running a command :
cprogram.c using gcc command
If you're on Linux, you can go straight to the source
ps uses: information about memory usage (among other things) is exposed on the
/proc filesystem. For example, try
cat /proc/1234/status as root, where 1234 is the pid of the process.
Note that this would likely not count memory used by gcc's subprocesses.