Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to know how to calculate CPU and memory utilization of a process using the /proc/stat and /proc/status files. Can I calculate the total memory and CPU a user is using?

share|improve this question
Note that there's no single definition of how much memory a process or a user is using. Some memory is shared between users or backed by disk files, for example the code of loaded executables and shared libraries. So don't be surprised if you end up with several different results (even for a single process). – Gilles Apr 23 '11 at 12:39
not just the code part of the shared libraries, even the data segments of the loaded executables and shared libraries can attribute to the memory consumption, however, it is important to note that how much incremental cost of the memory is being applied/used by the process. Look at pmap output of the process id. – Nikhil Mulley Jan 31 '12 at 10:01

ps is the simplest interface to the information in /proc.

Here's one way to list per user memory:

$ ps -e -o uid,vsz | awk '
{ usage[$1] += $2 }
END { for (uid in usage) { print uid, ":", usage[uid] } }'

If you really want to use proc, I would suggest using something like Python or Perl to iterate once over /proc/*/status, and store the user/usage key/value pair in a hash.

The relevant fields of /proc/PID/status appear to be:

Uid:    500     500     500     500
VmSize:     1234 kB

I think the four Uid numbers are real uid, effective uid, saved uid, and fs uid.

Assuming you want real uid, something like this should work:

# print uid and the total memory (including virtual memory) in use by that user
# TODO add error handling, e.g. not Linux, values not in kB, values not ints, etc.

import os
import sys
import glob

# uid=>vsz in KB
usermem = {}

# obtain information from Linux /proc file system
# http://www.kernel.org/doc/man-pages/online/pages/man5/proc.5.html
for file in glob.glob('[0-9]*'):
    with open(os.path.join(file, 'status')) as status:
        uid = None
        mem = None
        for line in status:
            if line.startswith('Uid:'):
                label, ruid, euid, suid, fsuid = line.split()
                uid = int(ruid)
            elif line.startswith('VmSize:'):
                label, value, units = line.split()
                mem = int(value)
        if uid and mem:
            if uid not in usermem:
                usermem[uid] = 0
            usermem[uid] += mem

for uid in usermem:
    print '%d:%d' % (uid,usermem[uid])

CPU is much harder.

The ps(1) man page says:

   CPU usage is currently expressed as the percentage of time spent
   running during the entire lifetime of a process. This is not ideal,
   and it does not conform to the standards that ps otherwise conforms to.
   CPU usage is unlikely to add up to exactly 100%.

So I'm not sure. Maybe look at top to see how it handles it. Or you could run ps -e -o uid,pid,elapsed twice at a given interval, and subtract the two times, or something.

Or install something that's better for this purpose, such as process accounting.

share|improve this answer
Great one ... never knew ps could do these things :) – Barun Apr 23 '11 at 7:49
thanx a lot.....i ran the script posted by you...it gives me the memory used by a user but when I computed the same thing with a utility named smem the results were different.The command I used was "smem -u" according to the man page of smem it gives me the memory used by the user.I want to know why there is difference in the output generated by the two.I would really be obliged. – nishan Apr 27 '11 at 3:44
@nishan: The smem home page explains this. In short: it depends on how you count virtual memory and shared libraries. Read about vsz versus rss versus pss for details. – Mikel Apr 27 '11 at 4:42

You can check the /proc/meminfo file:

cat /proc/meminfo | head -2
MemTotal:        2026816 kB
MemFree:          377524 kB

Using the above two entries you can calculate how much memory is being used at the moment:

cat /proc/meminfo | head -2 | awk 'NR == 1 { total = $2 } NR == 2 { free = $2 } END { print total, free, total - free }'
share|improve this answer
That's not per user. – Mikel Apr 23 '11 at 7:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.