Linux – How to measure total amount of memory used by userspace processes in Linux

linuxmemory

How to measure total amount of memory allocated to user space programs in Linux? That is size of all memory pages that userspace programs have in real memory.

/proc/meminfo doesn't seem to provide this info.

Currently I am adding rss fields of all processes from /proc/$pid/stat but that does not take into account shared memory pages.

Update: By "user space" I mean processes run by all users, including root (as opposed to kernel space).

Best Answer

Using smem to show a total of all user memory, no swap, and not counting any shared memory twice:

sudo smem -c pss -t | tail -1

Output on my system:

4119846

Unrolling that:

  • -c pss selects the column, in this case PSS. From man smem:

          smem reports physical memory usage, taking shared memory  pages
          into  account.   Unshared memory is reported as the USS (Unique
          Set Size).  Shared memory is divided evenly among the processes
          sharing   that  memory.   The  unshared  memory  (USS)  plus  a
          process's proportion of shared memory is reported  as  the  PSS
          (Proportional Set Size).  The USS and PSS only include physical
          memory usage.  They do not include memory that has been swapped
          out to disk.
    
    • -t shows a total or sum of all PSS used at the end, and tail -1 nips off the preceding data.

To show just the total unshared user memory, replace -c pss with -c uss:

sudo smem -c uss -t | tail -1

Output:

 3874356 

Note the above PSS total is more or less the same number as shown in row #5, column #2 here:

smem -w

Output:

Area                           Used      Cache   Noncache 
firmware/hardware                 0          0          0 
kernel image                      0          0          0 
kernel dynamic memory       1367712    1115708     252004 
userspace memory            4112112     419884    3692228 
free memory                  570060     570060          0 
Related Question