4

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).

  • Does "real memory" include swap devices? – agc Apr 13 '18 at 12:38
  • 1
    @agc: no, only memory that would be included in RSS but without duplicate shared pages. In other words, number of bytes on RAM sticks allocated to user processes. – el.pescado Apr 13 '18 at 12:43
2
+100

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 
  • Thanks for your answer. Is it possible to include root processes as well? I have updated my question. – el.pescado Apr 16 '18 at 6:12
0

This is a one-liner that I use, shows you what process uses how much Swap and the PID:

for file in /proc/*/status;
do
    awk '/VmSwap|Tgid|Name/ {printf "%s %s %s", $2, $3, $4} END { print "" }' "$file";
done | sort -k 3 -n -r | head -20

(I've split it here over several lines for readability. You can simply join all the lines together if you prefer it as one line.)

  • That code doesn't provide a total amount. – agc Apr 13 '18 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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