4

I typed the free command to get the memory usage as follows :

free -m 

output:

enter image description here

I want to use this command to get the same info but for all users for example :

enter image description here

I used this command .. because it easy for me .. to store its output in a variables in a bash scrip ...

Akari
  • 481

2 Answers2

5

One option is to use smem Install smem as free does not offer this functionality.

$ sudo smem -u -k -t
User     Count     Swap      USS      PSS      RSS 
daemon       1        0   196.0K   197.0K   360.0K 
rtkit        1        0   304.0K   317.0K     1.4M 
[...]
root        44        0   164.3M   197.7M   284.4M 
gert        88        0     1.7G     1.8G     3.2G 
---------------------------------------------------
           159        0     1.9G     2.1G     3.6G

For explanation on what USS and PSS means, here's an excerpt from the manpage of smem.

            [...] Unshared memory is reported as the USS (Unique Set
   Size).  Shared memory is divided evenly among the processes shar‐
   ing that memory.  The unshared memory (USS) plus a process's pro‐
   portion 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.

I suspect RSS is the Residential memory usage also referred to as RES in other utilities. For more information on expression of memory usage see this Q&A on Superuser.com: What I should know about memory management?

gertvdijk
  • 69,427
5

free is already based on the system-wide memory usage.

If you want something on a per-user basis, you could try something like:

ps aux | awk 'NR>2{arr[$1]+=$6}END{for(i in arr) print i,arr[i]}'

As a quick explanation of what the awk does:

  • Strips off the first line
  • Iterates through each line and creates an array of every given username. For each of those it adds the sixth column from ps aux (resident set size) and adds them up.
  • After that it just iterates through the array keys to print the content.
Oli
  • 299,380